Tutorial :Changing HTML DOM element name dynamically



Question:

What I'm thinking about is when I remove or add a child element from the DOM, I will change other elements' names through JavaScript or jQuery. For example, I'm going to remove the second text box from a form which have 3 text boxes. They have the names "input-1", "input-2" and "input-3." After removing "input-2," I'm going to rename "input-3" as "input-2". Then, when I'm going to add a new text box, it will be named "input-3." Is this efficient?


Solution:1

what is your html markup?... and why would you do that?...

$('input:text').attr('name',function(index,name){     return 'input-' + (index+1);     // this will rename input's of type text based on their indices..  });  


Solution:2

I just change the elements' names before the form is submitted to the server. No need for dynamic changing, i.e., when an element is removed and and a new one is added. It's more convenient.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »