user3446322
user3446322

Reputation: 129

how to add id's to tr using loop in jquery

I want to add differnt id's on tr using jquery. I am writing this code but its give unique id to all. Please help for it.

var tableTr = $('#edit-submitted-new-table-element tr');
    var countTr= tableTr.size();
    for(i=0; i <= countTr-1; i++){
        alert(i);
            tableTr.attr('id', i);

        }

Upvotes: 0

Views: 1571

Answers (2)

Oomph Fortuity
Oomph Fortuity

Reputation: 6148

You can use JQuery each to assign IDs to TR Element.It simply saves you the boilerplate code of incrementing the index and getting the value at that index.

    $('#edit-submitted-new-table-element tr').each(function(index){
       $(this).prop("id","id_"+index);
    });

Upvotes: 0

chestnut
chestnut

Reputation: 351

You add id to tableTr. It is a collection of tr, not one of the trs. Thanks to @C-linkNepal. id attribute can not start with number, you must add some letter before it.

tableTr.eq(i).attr('id', 'id' + i);

Upvotes: 5

Related Questions