<table>
<tbody>
<tr class="row-1 row-first">
<td><div class="inside">Text</div></td>
<td><div class="inside">Text</div></td>
<td><div class="inside">Text</div></td>
</tr>
<tr class="row-2">
<td><div class="inside"></div></td>
<td><div class="inside">Text</div></td>
<td><div class="inside">Text</div></td>
</tr>
<tr class="row-3 row-last">
<td><div class="inside"></div></td>
<td><div class="inside">Text</div></td>
<td><div class="inside">Text</div></td>
</tr>
</tbody>
</table>

That is my HTML.. I want to know the count of td's which does not have an empty div (with class="inside") in jQuery? How do I go about it?

link|flag

4 Answers

Something like this, with the has, not, and empty selectors:

$('td:not(:has(div.inside:empty))').length;
link|flag
I have a big selector line before the table comes into the html. classes -> .page-community-members .views-view-grid.grid-8... Shall I include it in the selector like this??? $('.page-community-members .views-view-grid.grid-8 td:not(:has(div.inside:empty))').length; – Pack Hack 2 hours ago
@Pack Hack - So? – lonesomeday 2 hours ago
Can i use it this way? $('.page-community-members .views-view-grid.grid-8 td:not(:has(div.inside:empty))').length; – Pack Hack 1 hour ago

Made a shorter version:

$("td div.inside:not(:empty)").length;

example here: http://jsfiddle.net/j7ucY/1/

link|flag
-1, It IS shorter, but it leaves out the class selector completely, and it will select the <div>s, not <td>s. You never know, in the actual application there might be multiple elements inside the table cells. – DarthJDG 1 hour ago
sorry, totally missed out on the class. added it now and its still 1 selector shorter than the other solution. – vdcruijsen.net 1 hour ago
$('td div.inside').filter(function(idx){
    return $(this).text() != ""
 }).length;

demo

link|flag

You can try this simple version.

$("td div.inside").length;

link|flag
-1, this solution completely ignores everything the poster asked for. No class selector, no checking for empty <div>s, nothing. – DarthJDG 1 hour ago

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.