That's very clever, Ian, I like that!

Here's another approach that uses a variable instead of the temporary class:

$(function() {
    $('tr').each( function() {
        var empty = true;
        $('td',this).each(function() {
            if( $(this).html() !== ' ' )
                return empty = false;
        });
        if( empty )
            $(this).addClass( 'allEmptyTds' );
    });
});

I think my favorite way to do this particular task, though, is with an
old-fashioned FOR loop:

$(function() {
    $('tr').each( function() {
        var $td = $('td',this);
        for( var i = 0, n = $td.length;  i < n;  ++i )
            if( $td[i].innerHTML !== '&nbsp;' )
                return;
        $(this).addClass( 'allEmptyTds' );
    });
});

(Warning: Untested code!)

-Mike

> From: Ian Struble
> Building on Karl's example and your new 
> all-td's-must-be-empty requirement; mark all the TR's with a 
> target class then sweep through the TD's and get rid of the 
> target class if you find a td that is not
> empty.   Play around a bit and see what else you can come up with.
> 
> $(document).ready(function() {
>   $('tr').addClass('allEmptyTds');    // mark
>   $('td').each(function() {
>     var $this = $(this);
>     if ($this.html() !== '&nbsp;') {
>       $this.parent().removeClass('allEmptyTds');  // and sweep
>     }
>   });
> });

> > From: Feed
> > > Hello all, I'm getting used to the excellent jQuery library and I 
> > > need some help of more experienced programmers. I have 
> this simple table:
> > >
> > > <table class="table">
> > >       <tr>
> > >               <td>content</td>
> > >               <td>content</td>
> > >               <td>content</td>
> > >       </tr>
> > >       <tr>
> > >               <td>&nbsp;</td>
> > >               <td>&nbsp;</td>
> > >               <td>&nbsp;</td>
> > >       </tr>
> > >       <tr>
> > >               <td>content</td>
> > >               <td>content</td>
> > >               <td>content</td>
> > >       </tr>
> > > </table>
> > >
> > > What I need is to add a class to the TRs that have 
> > > children TDs that have &nbsp; inside (and ONLY
> > > &nbsp;)... I'm having problems because &nbps;
> > > is not text, it's html code...

Reply via email to