Take the 2-minute tour ×

I have table

  <td colspan="3"></td>
 <tr> //select this
 <tr> //select this

so for the above table i want to select all trs which contains 3 tds

how could i do that with jquery?

basically i would need to get the count of such rows.

share|improve this question
add comment

3 Answers

You can use .filter() [docs]:

$('table tr').filter(function(){
    return $(this).children('td').length === 3;

Notice: @mu's answer is more concise and will perform better in modern browsers. I will still leave this answer as a general demonstration for .filter().

share|improve this answer
+1 Exactly what I was launching JSfiddle to try :) –  jensgram Sep 6 '11 at 8:10
I think your second example is invalid, as colspan belongs on a td or th, not a tr –  Adam Hopkinson Sep 6 '11 at 8:13
@adam: You are right, but the selector would still work. The colspan attribute will just have no meaning on table rows. –  Felix Kling Sep 6 '11 at 8:16
@mu's answer selects trs having three OR MORE tds, yours does precisely what OP asked for. –  Michael Sagalovich Sep 6 '11 at 8:40
@Michael: True... I guess it depends on actual HTML you have. If three is the maximum number of tds anyway, then it's fine. –  Felix Kling Sep 6 '11 at 9:09
show 1 more comment

You could select the third <td> in each <tr> and the jump up a level to get the parent:

$('tr td:nth-child(3)').parent();

Using :nth-child in this case is safe because your <tr>s should only have <td> children.

For example:

h = $('<table><tr colspan="3"><td></td></tr><tr id="x"><td><span>x</span></td><td></td><td></td></tr><tr id="y"><td></td><td></td><td></td></tr></table>');
trs = h.find('tr td:nth-child(3)').parent();
// trs[0] is <tr id="x">
// trs[1] is <tr id="y">

And a demo provided by Felix Kling (now that jsfiddle.net is back in action): http://jsfiddle.net/6kMTE

share|improve this answer
+1 Good one.... and using CSS selectors only :) –  Felix Kling Sep 6 '11 at 8:21
@Felix: Shame that jQuery doesn't understand :nth-of-type so that this trick would be applicable outside the strict element nesting of a <tr>. –  mu is too short Sep 6 '11 at 8:25
Here is a fiddle: jsfiddle.net/6kMTE –  Felix Kling Sep 6 '11 at 8:26
@Felix: Thanks, jsfiddle has been rather slow tonight, I had to test it in Firebug. –  mu is too short Sep 6 '11 at 8:34
add comment

You can use:


which selects any tr containing a td in position 3 (js is zero-based).

Note your html is invalid - the colspan belongs on the td, not the tr

share|improve this answer
add comment

Your Answer


