5

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?)
        }
    }); 
}
3
  • How about you show us your code and what you have already tried doing? Commented Jul 26, 2013 at 7:14
  • 2
    From Wikipedia: jQuery is a multi-browser (cf. cross-browser) JavaScript library designed to simplify the client-side scripting of HTML. jQuery is not for querying databases. The link you post refers to an HTML table not a database table. Commented Jul 26, 2013 at 7:17
  • I am also refering to an HTML table @Matt Commented Jul 26, 2013 at 8:56

2 Answers 2

14

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');
        }
    });
});
Sign up to request clarification or add additional context in comments.

3 Comments

please, take a look at my edit. actually it is my fault, I should have explained my situation briefly for you to give a good answer :)
with your code, I only need to add return $(this).index() thanks
Any shorter version of this? seems :contains() does not work for such kind of work.
4

You can achieve that with your database query:

SELECT (id, name, age, location) FROM table WHERE id = 3;

If you want to use filters on page, you can you the first answer of your posted question:

var tableRow = $("td").filter(function() {
    return $(this).text() === "3";
}).closest("tr");

Explanation:

  1. Make a var tableRow
  2. Find all cells with $("td")
  3. Filter the found cells based on a function, this is testing whether the content of the cell is equal to '3'. $(this) refers to the cell, .text() outputs the text content.
  4. closest("tr") is added to select the tr where the td is placed in.
  5. If you want to do something with that row, you can do this: tablerow.hide().

To hide all rows, but not the selected row, use this code:

$('tr').not(tableRow).hide();

Working demo: http://jsfiddle.net/hzW2a/

1 Comment

I cannot achieve that with my database query, because I need to add a class on that found row :)

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.