1

I have a table with a list of names in column 1 and i'm looking to find the row index if the table contains a certain value.

What I've tried:

var nameToSearch = "Bob";
var index = $("#table-names tr").index(nameToSearch);
console.log(index);

But I just keep getting the same result "-1" indicating the value was not found. Although I know the value is in there. Am I getting something wrong?

2
  • 1
    Can you post the HTML for your table? Commented Jan 3, 2014 at 22:09
  • 2
    You should pass an element to the jQuery .index() method. Commented Jan 3, 2014 at 22:10

2 Answers 2

2
var nameToSearch = "Bob";
var elem = $("#table-names tr:contains("+ nameToSearch +")");
var index = elem.index('#table-names tr');
console.log(index);
Sign up to request clarification or add additional context in comments.

2 Comments

perfect, many thanks. 7 minutes to accept the answer, will do it as soon as i can.
no need to pass a selector. the default is to index it in relation to its siblings..
0

Try

$("#table-names tr:contains("+nameToSearch+")").index();

or a bit more safe

$("#table-names tr")
        .filter(function(){
             return $(this).text().indexOf(nameToSearch) > -1;
        }).index();

Comments

Your Answer

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