Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping through <tr> when table ID is passed

Tags:

jquery

I have four HTML tables and have to compare data from one table with one that is selected by the user. I am passing the user-selected table ID into this function, but I don't know how to loop through the rows of this table:

function callme(code) {

    var tableName = 'table'+code;
    //alert(tableName);

    //How to do loop for this table?? HELP!!
    $('#tableName tr').each(function() {   //how to do
        if (!this.rowIndex) return; // skip first row

        var customerId = $(this).find("td").eq(0).html();
        alert(customerId);
        // call compare function here.
    });
}

It should be something very simple for a experienced jQuery programmer. Here is my jsfiddle: http://jsfiddle.net/w7akB/66/

like image 676
sana Avatar asked Aug 09 '12 15:08

sana


2 Answers

You are using bad selector, this:

$('#tableName tr')

means get all tr from table with id tableName. This is what you want to do:

$('#' + tableName +' tr')

so you will select table with id stored inside tableName variable.

like image 192
Zbigniew Avatar answered Oct 23 '22 05:10

Zbigniew


The selector is just a string. You can concatenate the pieces together with your variable:

$('#' + tableName + ' tr').each(function() {
    // ...
});

I updated your jsfiddle with this change.

like image 27
FishBasketGordo Avatar answered Oct 23 '22 07:10

FishBasketGordo