how do i clonenode for example an div tag where are one text input field and two option tags to make that new cloned input field with an empty value? Thank you.
Cloning an element but text input with empty value
Collapse
X
-
i would suggest to use the cloneNode() method and remove the unwanted attributes after the clone-operation ...
kind regards -
i tried some things but none works
this is my function
Code:function addinput(inputid,what) { var obj = document.getElementById(what).cloneNode(true); document.getElementById(inputid).appendChild(document.createElement('br') ); document.getElementById(inputid).appendChild(obj); }
Comment
-
Code:<fieldset class="border" id="move_photo"> <legend>Presunúť fotku v albume:</legend> <label for="move_photo_id">ID fotky:</label><br /> <div id="6"> <span id="sprytextfield6"> <input type="text" name="move_photo_id[]" value="" /> <span class="textfieldInvalidFormatMsg">Iba cifry prosÃm. </span></span><br /> <select name="dozadualebodopredu[]" class="w"> <option value="dozadu">Dozadu</option> <option value="dopredu">Dopredu</option> </select> <br /> <select name="okolko[]" class="w"> <option value="1">O 1 miesto</option> <option value="16">O 16 miest</option> </select> </div> <input class="button" type="button" value="Pridať pole" onClick="return addinput('move_photo', '6')" /> </fieldset>
Comment
-
i tried it, and the function now looks like
Code:function addinput(inputid,what) { var obj = document.getElementById(what).cloneNode(true); document.getElementById(inputid).appendChild(document.createElement('br') ); document.getElementById(inputid).appendChild(obj); document.getElementById(what + '[]').value = ''; }
"+ '[]'"Comment
-
ahhh ... i overlooked the id ... note ... we want to use the id of the input field:
Code:document.getElementById(what + '_id[]').value = '';
kind regardsComment
-
great, thanks, i made it work now, but it doesnt do what i wanted, this removes value from my first original text input, but i need to remove it from the cloned input.
this is my function now
Code:function addinput(where,what,field) { var obj = document.getElementById(what).cloneNode(true); document.getElementById(where).appendChild(document.createElement('br') ); document.getElementById(where).appendChild(obj); document.getElementById(field).value = ''; }
Code:<fieldset class="border" id="add_friend"> <legend>Pridať frienda do contact listu:</legend> <label for="add_friend_uid">User ID frienda:</label><br /> <span id="sprytextfield4"> <input type="text" name="add_friend_uid[]" id="add_friend_uid[]" value="" /> <span class="textfieldInvalidFormatMsg">Iba cifry prosÃm. </span></span> <input class="button" type="button" value="Pridať pole" onClick="return addinput('add_friend', 'sprytextfield4', 'add_friend_uid[]')" /> </fieldset>
Comment
-
in any case ... when you clone a node and want to use dom-methods later on you would need to unify te ids ... otherwise you will encounter problems with ids that have to be unique. so you would need to loop through the cloned elements and adapt the ids to be unique.
kind regardsComment
-
as i said ... loop through the child-nodes - here is an example ... it should work for you. it calls a method recursivly to find input-nodes of type text and cleans the value:
Code:<script type="text/javascript"> function addinput(where, what) { var obj = document.getElementById(what).cloneNode(true); var br = document.createElement('br'); cleanUpInputs(obj); document.getElementById(where).appendChild(br); document.getElementById(where).appendChild(obj); } function cleanUpInputs(obj) { for (var i = 0; n = obj.childNodes[i]; ++i) { if (n.childNodes && n.tagName != 'INPUT') { cleanUpInputs(n); } else if (n.tagName == 'INPUT' && n.type == 'text') { n.value = ''; } } } </script>
Comment
Comment