2

I have a function which finds a row within an html table whos first column contains a specific value...

$('#table tr').find('td:eq(0):contains(' + value + ')').parent();

The problem I have is that it returns all rows that have the number 1 in column 0 rather than just the row with 1. (ie, 1, 15, 21 etc..)

Is there an equivalent 'equals' to the 'contains' part of this search or some other way of doing this? I can't seem to find one.

1 Answer 1

2

The simplest would be to use filter :

$('#table tr').filter(function(){
   return $.trim($('td', this).eq(0).text())=="1";
});

Demonstration

4
  • 2
    need find or nth-child for all rows $('#table tr td:eq(0)') will return length=1
    – charlietfl
    Jan 21, 2013 at 13:15
  • selector only locates a single td , won't loop all rows and look at first td in row jsfiddle.net/6HnEy
    – charlietfl
    Jan 21, 2013 at 13:26
  • made same mistake numerous times myself
    – charlietfl
    Jan 21, 2013 at 13:30
  • 1
    Thank you! You've solved the problem, there are a good few people sending mental hugs your way at the moment (it was one of those last minute bugs before a release). Incidentally the first solution didn't work, it seemed to return the entire table, but the second was spot on. Thanks again :)
    – Captastic
    Jan 21, 2013 at 13:38

Your Answer

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

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