Hello everyone, im new to this, so forgive me if I make any mistakes. Basically, I have a created a JavaScript code to add new form elements when the user clicks the button. I have one glitch, and it is that the new form elements always contain information entered into the initial form. Is there a way to create these fields with them being empty?
the problem im having is if i chqnge their values, its still copying whatever is in the initial fields into the new created ones. It's probably a minor mistake but i cant seem to solve this. Please help!
you must clone the element before the changes. That means you must perform the clone operation onload, than to repeat the cloning each time immediately after the append.
Take care... I see in your code that you clone an element with a certain id. You must change that id for the clone (and all the ids of its childnodes) because the id must be unique on document/session. You probably will need to change the name attributes as well, if the server-side application (to whom you might submit data) is not instructed to work with arrays of names in query.
iv changed my JavaScript to this and it works great now:
<script type="text/javascript">
var counter = 0;
function moreFields() {
counter++;
var newFields = document.getElementById('childinfo').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 + counter;
}
var insertHere = document.getElementById('writeroot');
insertHere.parentNode.insertBefore(newFields,insertHere);
var aInput=newFields.getElementsByTagName('input');
aInput[0].value=' ';
}
I added the line var aInput=newFields.getElementsByTagName('input');
aInput[0].value=''; i got this line from one of your previous posts Fang, and it worked really well...thank you all for your help.
</script>
Bookmarks