Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching a table row for specific text in a COLUMN with jQuery

Tags:

html

jquery

I found this question on StackOverflow that has a very nice answer, but being a beginner, I want a more detailed explanation, because my situation is a bit different: I have a table like the one below:

 ________________________________________________
 | id |     name     | age |      location      |
 |----+--------------+-----+--------------------|
 |  1 | Victor       | 14  | Bucharest, Romania |
 |  2 | John         | 17  | New York, USA      |
 |  3 | Steve        | 12  | New York, USA      |
 |  7 | Michael      | 37  | Washington DC, USA |
 |  9 | Michaela     | 25  | Washington DC, USA |
 |----+--------------+-----+--------------------|

The id of the person is in my database (MySQL database) set to AUTO_INCREMENT, so if I delete a record, it will be like the last one in my table here (from 7 to 9). I want to search the row that has the personId = 3 in my HTML table.

So, based on the question in my link, how can I refine that search to be only in the ID column?

Edit for @yeyene:

This is the function based on your code. Actually, I don't need to change the background color of the row, I need the row's index for using it later with .eq(index) to make some operations on it:

function findTableRow(personId){
    $('#people tr').each(function(){
        if($(this).find('td').eq(0).text() == personId){
            return (row's index?)
        }
    }); 
}
like image 856
Victor Avatar asked Dec 03 '22 22:12

Victor


1 Answers

Check here, DEMO http://jsfiddle.net/yeyene/wAxNu/

$(document).ready(function(){
    $('table tr').each(function(){
        if($(this).find('td').eq(0).text() == '3'){
            $(this).css('background','red');
        }
    });
});
like image 146
yeyene Avatar answered May 30 '23 05:05

yeyene