views:

35

answers:

2

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?

A: 

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..
});
Reigel
A: 

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.

jean27