javascript - Show/hide table row if a child DIV is empty -
i have table multiple entries. after each entry table row edit table row, column spanned cell, inside of div html dynamically loaded into. problem having these empty table rows causes lot of whitespace appear on page when it's rendered.
i understand can't dynamically load html directly cell, have div in load content into.
i want hide table row while child div in empty, , show table row once information has been dynamically loaded child div. dynamically loaded information can removed need hidden again once it's empty again.
<table width="100%"> <tbody> <tr> <td>a</td> <td>b</td> <td>c</td> </tr> <tr style="display: none;"> <td colspan="3"><div></div></td> </tr> </tbody> </table> $("tr").each(function() { if (this.children().filter(":empty").length) { this.hide(); } else { this.show(); } });
the
div
isn't child, it's grandchild,children()
won't finddiv
s. use context orfind
instead.you operating
hide
,show
on dom element, not jquery element. need wrap in jquery first.
therefore, run code everytime load something:
//find empty divs , hide it's tr $("div:empty").closest('tr').hide(); //find non-empty divs , show it's tr $("div:not(:empty)").closest('tr').show();
and ma! no loops! no each
! :d
Comments
Post a Comment