简体   繁体   中英

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.

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++;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM