Erdem Gundogdu
Erdem Gundogdu

Reputation: 297

How to define an id for the added rows in a table in JS?

i have to add rows to a table dynamically,i can add rows and delete.But how can i define an id for the added rows to write something inside of them or add some links ?

Upvotes: 0

Views: 84

Answers (2)

Jeremy Miller
Jeremy Miller

Reputation: 383

You would have to add the new id number to the tr when you create the row. If you're using jQuery you could do something like this:

$('#btnAdd').click(function () {
        var num = $('tr').length;
        var newNum = new Number(num + 1);
        $('table').find('tr:first-child').append().after('<tr id="row-' + newNum + '"><td>Some new data</td></tr>');   
    });

This would count how many <tr> you have and add this as the highest number to your new <tr>. So if you have 5 <tr>, then the newest would be id="row-6" etc.

I haven't tested that code, but that SHOULD work. You may need to play around with the insert line a bit to get it exactly where you want it. But that should be starting point.

Upvotes: 0

Etienne Dupuis
Etienne Dupuis

Reputation: 13796

If it is always the last row, your could use jQuery and something like :

$('table tr:last')

Upvotes: 1

Related Questions