Skip to content
Advertisement

Making a counter using JavaScript/JQuery clone method in an HTML table

I need to make a counter using JavaScript/JQuery with clone method in the second column like for example the first row 1 and when I click on add button it automatically display number 2. I am using clone method in JavaScript/JQuery and I don’t know how to add this. This is my full code:

JavaScript
JavaScript

Advertisement

Answer

Consider the following.

JavaScript
JavaScript

There is no need for a Counter when you can just request the current length of a selector. For example, you can get the length of all the Rows in the Table Body. Initially, that is 1. The next one would be 2.

Now if the table has a unique start, lets say 20, then you would want to get that String value, cast it as an Integer, and increment that value.

JavaScript

This would result in 21.

Update

Based on your comment, when you delete a row, you want the numbers to remain continuous. This means you need to redraw all or at least all further numbers.

JavaScript

You would then run this function directly after a Row was removed.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement