4
<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?

3 Answers 3

10

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

$('td:not(:has(div.inside:empty))').length;
Sign up to request clarification or add additional context in comments.

2 Comments

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;
Can i use it this way? $('.page-community-members .views-view-grid.grid-8 td:not(:has(div.inside:empty))').length;
4

Made a shorter version:

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

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

3 Comments

-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.
sorry, totally missed out on the class. added it now and its still 1 selector shorter than the other solution.
This will break if there is, for instance, a br or an img element within the div, since they are inherently empty.
0
$('td div.inside').filter(function(idx){
    return $(this).text() != ""
 }).length;

demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.