HTML and JavaScript auto increment number for Table

HTML and JavaScript auto increment number for Table:
 
<table>
  <tr>
    <td>Raj</td>
  </tr>
  <tr>
    <td>Kiran</td>
  </tr>
  <tr>
    <td>Peter</td>
  </tr>
  <tr>
    <td>Mahesh</td>
  </tr>
</table>
<script>
    var tables = document.getElementsByTagName(‘table’);
    var table = tables[tables.length -1];
    var rows = table.rows;
    for(var i = 0, td; i < rows.length; i++){
        td = document.createElement(‘td’);
        td.appendChild(document.createTextNode(i + 1));
        rows[i].insertBefore(td, rows[i].firstChild);
    }
</script>
🔥24 Views
0 0 vote
Article Rating

Anu

Do you have anything to Ask?

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments