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.
19 8223 gits 5,390
Expert Mod 4TB
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 -
function addinput(inputid,what)
-
{
-
var obj = document.getElementById(what).cloneNode(true);
-
document.getElementById(inputid).appendChild(document.createElement('br') );
-
document.getElementById(inputid).appendChild(obj);
-
}
-
where to put removeAttribute correctly?
gits 5,390
Expert Mod 4TB
how does your cloned node look like?
-
<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>
gits 5,390
Expert Mod 4TB
i guess you want to drop the value of the 'move_photo_id[]'-node? then you could just use: - document.getElementById(what + '[]').value = '';
in case the id with the square brackets follow this rule?
kind regards
i tried it, and the function now looks like -
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 = '';
-
}
-
It looks it could work but it doesn't ;[ I tried also removing
"+ '[]'"
gits 5,390
Expert Mod 4TB
ahhh ... i overlooked the id ... note ... we want to use the id of the input field: - document.getElementById(what + '_id[]').value = '';
in your example the parameter what is 'move_photo' and the input node id is 'move_photo_id[]' ...
kind regards
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 -
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 = '';
-
}
-
and node: -
<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>
-
gits 5,390
Expert Mod 4TB
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 regards
but i dont wont to use it later, i want to just create another text input with empty value by clicking on a button. best would be by using clonenode because it copies other elements too. help pleasee
gits 5,390
Expert Mod 4TB
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: - <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>
kind regards
yees!! man!! it finaly works! thanks a lot to you, you are the only one who helped me out, I appreciate it. Nice :)
gits 5,390
Expert Mod 4TB
no problem :) ... that's why i'm here for ... in case you have anymore questions, just post back to the forum ...
kind regards
ok so one more question, to make this better, how could i give this new cloned input a focus? in other words, each click on add input button, would also make focus on latest cloned input.
gits 5,390
Expert Mod 4TB
after the line where we set the value to an empty value you could add:
kind regards
I tried that already, wrote it under this line and it's not working, something is wrong I don't know what. gits 5,390
Expert Mod 4TB
ahhrg ... we need to focus the node after appending it of course :) ... here is the adaption: - <script type="text/javascript">
-
-
function addinput(where, what) {
-
var obj = document.getElementById(what).cloneNode(true);
-
-
var node = cleanUpInputs(obj);
-
-
document.getElementById(where).appendChild(document.createElement('br'));
-
document.getElementById(where).appendChild(obj);
-
-
node.focus();
-
}
-
-
function cleanUpInputs(obj) {
-
var focusNode = null;
-
-
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') {
-
focusNode = n;
-
n.value = '';
-
}
-
}
-
-
return focusNode;
-
}
-
-
</script>
-
kind regards
thanks, works great now :)
gits 5,390
Expert Mod 4TB
of course it does :) ... just kidding :) ... glad to hear it works for your problem ...
kind regards
Sign in to post your reply or Sign up for a free account.
Similar topics
by: tornado |
last post by:
Hi all,
I am pretty new to PHP. I was reading PHP manual and trying out the
example from 2nd
chapter (A simple Tutorial). When i try to print the variable as
given in the example it returns...
|
by: Kai Grossjohann |
last post by:
I would like to put a text input field (in the sense of <input
type="text">) and an image next to each other, where I know the size
in pixels of the image, and I know the total width in em. I...
|
by: BiNZGi |
last post by:
Hi
I have reduced the problem to this code:
<form>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td><input type="text" style="width: 100%;" value="Lorem ipsum
dolor...
|
by: Lukas |
last post by:
title:
xml to xml mapping: empty elements output although input element is not
empty
Why is is that when mapping from a XML schema to another XML schema,
when drawing a default connection...
|
by: ina |
last post by:
Hello all,
I am newbie in xml and have a problem with this parse.
I have this xml.file
<Style>
<Strategy>
|
by: Diogenes |
last post by:
Hi All;
I, like others, have been frustrated with designing
forms that look and flow the same in both IE and Firefox.
They simply did not scale the same.
I have discovered, to my chagrin,...
|
by: John Kotuby |
last post by:
Hi all,
I have a simple user-form which accepts contact and profile information for
the user to edit and save. All the textboxes are working fine and displaying
the expected data, both when the...
|
by: Mike Harrison |
last post by:
Hi, I have some simple HTML like this:
<div id="container" style="width:100%;">
<input type="text" <input type="button" style="float:right;"
value="Click here...">
</div>
I want the button...
|
by: pazazuzu |
last post by:
Hi everyone,
I currently have an assignment which I am almost done with but am having problems trying to figure out how to integrate a string function search. I have a text box called...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |