Skip to content
Advertisement

Change table row ID dynamically when delete in jquery

html

JavaScript

Jquery

JavaScript

https://jsfiddle.net/u3hmfc7x/1/

This will dynamically add table rows or delete the row when I click the button. After that, if user deleting the second row, then the row id 2 has been deleted and row id should be interchanged dynamically. Does anyone know how to fix this :(?

For example

JavaScript

If user delete the second, the rest will auto sequence back the ID, it will become as below

JavaScript

Advertisement

Answer

I think a wiser option, instead of changing the ID, would be to swap the values. You can do that by changing your onclick for delete operation to:

JavaScript

Here’s the code for that: https://jsfiddle.net/ckpLqs4g/

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