Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-17-2011, 03:27 PM   PM User | #1
apprentice
New to the CF scene

 
Join Date: Feb 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
apprentice is an unknown quantity at this point
Change the value of an attribute in array

I have a form with dynamic fields. Javascript is used to clone multiple fields. Each time a set of field is cloned, I am trying to set a unique name for each attribute .e.g yourname[], yourname_1[] yourname_2[]. The corresponding line is (newField[i].setAttribute("name", theName + "_" + i ) however its not seem to be working, values remain the same. What i'm I doing wrong?

Code:
var counter = 0;
function moreItems() {
   counter++;
   var newFields = document.getElementById('readRoot').cloneNode(true);
   newFields.id = '';
   newFields.style.display = 'block';
   var newField = newFields.childNodes;
   for (var i=0;i<newField.length;i++) {
      var theName = newField[i].name;
      if (theName){
         newField[i].name = theName + "_" + i;
		newField[i].setAttribute("name", theName + "_" + i );
		 
		 }
		     }
   var insertHere = document.getElementById('writeroot');
   insertHere.parentNode.insertBefore(newFields,insertHere);
}

//window.onload = moreItems;

HTML Form

<div id="readRoot">
<p>Your Name: <input id="text" name="yourname[]" />

<br />
E-mail: <input id="text" name="email[]"/>
<br />
Fav Colour: <input id="text" name="colour[]" /></p>
</div>

<span id="writeroot"></span>
<input type="button"  value="Add Another Item"  onclick="moreItems()" />

<p>Your comments:<br />
<textarea id="comments" name="comments" rows="10" cols="40"></textarea></p>


<p><input type="submit" value="Send it!"></p>
</form>

</body>
</html>

Last edited by apprentice; 02-17-2011 at 08:26 PM..
apprentice is offline   Reply With Quote
Old 02-17-2011, 07:40 PM   PM User | #2
venegal
Gütkodierer


 
Join Date: Apr 2009
Posts: 2,127
Thanks: 1
Thanked 426 Times in 424 Posts
venegal has a spectacular aura aboutvenegal has a spectacular aura about
The things you are looping over are not actually the inputs, but the child nodes of #readRoot, which are two empty text nodes and the <p>, which contains the inputs.

Also, even if you make this work, the way you have it set up the resulting names will be like "yourname[]_1", and not "yourname_1[]". And why do you need those brackets anyway if you're manually numbering the fields?

Oh, and please put your code in [CODE][/CODE] tags, so it's easier to read.
venegal is offline   Reply With Quote
Old 02-17-2011, 08:19 PM   PM User | #3
apprentice
New to the CF scene

 
Join Date: Feb 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
apprentice is an unknown quantity at this point
Hi

It doesnt actually matter if the output is yourname_1[] or yourname[]_1. I am trying to make the names unique. The brackets are not a big deal, they can be removed.

Last edited by apprentice; 02-17-2011 at 08:27 PM..
apprentice is offline   Reply With Quote
Reply

Bookmarks

Tags
javascript, setattribute

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:12 PM.


Advertisement
Log in to turn off these ads.