473,411 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

javascript: adding form elements

Hi,
I can't get my semi-dynamic form to work. Maybe you could help.

I've got a simple form with a select box. And a button that, when
clicked, creates another copy if the select box and places it in a
specified div.

<select name="tst[]" id="tst">
<option value=1>1</option>
<option value=2>6</option>
</select>
<input type="button" value="Add"
onclick="createNew(document.getElementById('tst'),
document.getElementById('pasl'));">

here's the script:
function createNew(sel, div) {
spNew = document.createElement("span");
selNew = sel.cloneNode(true);
spNew.appendChild(selNew);

spNew.innerHTML = spNew.innerHTML + "<input type=\"button\"
value=\"Drop\"
onclick=\"alert(\'ok\');\" id=\"btn\" >";

sp = div.appendChild(spNew);
};

It works ok in IE, but under firefox, the value of the new select is
not submitted. The div that I add the select to is a child of the form,
so this should work, right? What could I have missed?

Another question would be: what is the correct way to specify the event
for a dinamically created button?

Thanks in advance,
Domas

Dec 14 '05 #1
4 2029
domas wrote:
Hi,
I can't get my semi-dynamic form to work. Maybe you could help.

I've got a simple form with a select box. And a button that, when
clicked, creates another copy if the select box and places it in a
specified div.

<select name="tst[]" id="tst">
<option value=1>1</option>
<option value=2>6</option>
</select>
<input type="button" value="Add"
onclick="createNew(document.getElementById('tst'),
document.getElementById('pasl'));">

here's the script:
function createNew(sel, div) {
spNew = document.createElement("span");
You should test for support for such features before using them. There
does not seem to be any reason to make spNew (or selNew) global, so keep
them local with the 'var' keyword:

var spNew = document.createElement("span");
var seNew = ...

selNew = sel.cloneNode(true);
You now have two selects with the same ID, which will create invalid
markup as soon as you add the second one to the document. The identical
names may be an issue too (or not).

selNew.id = 'someNewId';

spNew.appendChild(selNew);

spNew.innerHTML = spNew.innerHTML + "<input type=\"button\"
value=\"Drop\"
onclick=\"alert(\'ok\');\" id=\"btn\" >";
Not a good idea to mix non-standard innerHTML and DOM, particularly when
adding onclick attributes:

var inpNew = document.createElement('input');
inpNew.type = 'button';
inp.onclick = function (){alert('OK')}
ip.id = 'btn';
spNew.appendChild(inp);


sp = div.appendChild(spNew);
Here you create another global variable 'sp' that is a reference to the
newly added node (i.e. spNew). If it isn't used for anything, then:

div.appendChild(spNew);
will do the job.

};

It works ok in IE, but under firefox, the value of the new select is
not submitted. The div that I add the select to is a child of the form,
so this should work, right? What could I have missed?

Another question would be: what is the correct way to specify the event
for a dinamically created button?


I guess you mean add an event - you can use the method above, or use
addEventListener/attachEvent, the above is simpler and usually sufficient.

You can also do something like:
function createNew(...) {

...
inp.onclick = sayHi;
...

}

function sayHi(){
alert('OK');
}

--
Rob
Dec 14 '05 #2
thanks. But it didn't help. I just tried a small experiment: I added a
simple text input box to the div and its value does not get submitted
either.
It seems that I need to somhow attach the dynamically created input to
the form.

Thanks,
Domas

Dec 14 '05 #3
domas wrote:
thanks. But it didn't help. I just tried a small experiment: I added a
simple text input box to the div and its value does not get submitted
either.
It seems that I need to somhow attach the dynamically created input to
the form.


Here's a quick 'n dirty example, it will only add one input properly but
shows the basics.

You need to specify unique IDs for each new element, plus the reference
to the node to clone is by name so you need to create new names too
(note that if you have multiple controls with the same name,
form.controlName will return a collection not a single element):
<script type="text/javascript">

function addInput(el){
if (!document.createElement) {
alert('Sorry, my script doesn\'t support creating new elements'
+ ' for your browser');
return;
}

var d = el.parentNode; // d is now a reference to 'divA'
var newInput = el.cloneNode(false);
newInput.value = '';

// Make sure unique IDs are specified for all elements
newInput.id = 'textB';
newInput.name = 'textB';
d.appendChild(newInput);

var newBut = document.createElement('input');
newBut.type = 'button';
newBut.value = 'Say blah';
newBut.onclick = function(){alert('blah')}
d.appendChild(newBut);
}

</script>

<form action="">
<div id="divA">
<input type="text" name="textA" id="textA">
<input type="button" value="Add input"
onclick="addInput(this.form.textA)"><br>
</div>
<div>
<input type="submit">
</div>
</form>

--
Rob
Dec 14 '05 #4
RobG wrote:
domas wrote:
spNew.appendChild(selNew);

spNew.innerHTML = spNew.innerHTML + "<input type=\"button\"
value=\"Drop\"
onclick=\"alert(\'ok\');\" id=\"btn\" >";


Not a good idea to mix non-standard innerHTML and DOM, particularly when
adding onclick attributes:

var inpNew = document.createElement('input');
inpNew.type = 'button';
inp.onclick = function (){alert('OK')}
ip.id = 'btn';
spNew.appendChild(inp);


While I strongly agree with you about that (and the rest of your posting),
it has to be clarified that

1. The term "DOM" is not and has never been restricted to "W3C DOM":
`innerHTML' is a part of proprietary DOMs. Therefore the statement
"Do not mix non-standard innerHTML and DOM." is nonsense :)

2. Assigning a Function object reference to `inp.onclick' is not more or
less standard than `innerHTML' is. The HTMLInputElement interface does
not have an `onclick' attribute in W3C DOM Level 2 HTML and W3C DOM
Level 2 Events does not specify that attribute either. If you would
want to be strictly standards compliant, you would have to write

inp.addEventListener('click', functionObjectReference, false);

but then you would run into problems with IE, for example.
Regards,
PointedEars
Dec 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: | last post by:
The problem lies here eval("document.TeeForm.amt.value(S+M)"); S and M suppose to add up and the total suppose to appear on the AMT field but it didn't. Any help? ...
2
by: sorobor | last post by:
dear sir .. i am using cakephp freamwork ..By the way i m begener in php and javascript .. My probs r bellow I made a javascript calender ..there is a close button ..when i press close button...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
Oralloy
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,...
0
tracyyun
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...
0
agi2029
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,...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.