Quoting via <tr> when the table identifier is passed

I have four HTML tables and have to compare data from one table with the one selected by the user. I pass the table id selected by the user to this function, but I don't know how to iterate over 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 an experienced jQuery programmer. Here is my jsfiddle: http://jsfiddle.net/w7akB/66/

+5
source share
2 answers

You are using a bad selector, this is:

$('#tableName tr')

means get everything trfrom the table with id tableName. This is what you want to do:

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

so that you select a table with an identifier stored inside the variable tableName.

+6
source

- . :

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

jsfiddle .

+3

All Articles