Dinesh G Dinesh G - 7 months ago 10
Javascript Question

How to add value dynamically in table cell using jquery?

For Example i have an table filled with values

-----------------------------------------
S.No | Name | Address
-----------------------------------------
| Dinesh | Salem
-----------------------------------------
| Senthil | Chennai
-----------------------------------------
| Sundar | Namakkal
-----------------------------------------


Actually i need to fill
serial no
to each row dynamically using jquery.

var i = 1;
$('#tbl td').each(function() {
$(this).append(i);
i++;
});


I need the output should be

-----------------------------------------
S.No | Name | Address
-----------------------------------------
1 | Dinesh | Salem
-----------------------------------------
2 | Senthil | Chennai
-----------------------------------------
3 | Sundar | Namakkal
-----------------------------------------


Please help me to solve this problem.

Answer

If you wanna replace first td , you can use :first-child

Try like this

var i=1;
$('#tbl td:first-child').each(function() {
    $(this).html(i);
    i++;
});