473,666 Members | 1,989 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="create New(document.ge tElementById('t st'),
document.getEle mentById('pasl' ));">

here's the script:
function createNew(sel, div) {
spNew = document.create Element("span") ;
selNew = sel.cloneNode(t rue);
spNew.appendChi ld(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 2039
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="create New(document.ge tElementById('t st'),
document.getEle mentById('pasl' ));">

here's the script:
function createNew(sel, div) {
spNew = document.create Element("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.create Element("span") ;
var seNew = ...

selNew = sel.cloneNode(t rue);
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.appendChi ld(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.create Element('input' );
inpNew.type = 'button';
inp.onclick = function (){alert('OK')}
ip.id = 'btn';
spNew.appendChi ld(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
addEventListene r/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.controlNam e will return a collection not a single element):
<script type="text/javascript">

function addInput(el){
if (!document.crea teElement) {
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(fa lse);
newInput.value = '';

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

var newBut = document.create Element('input' );
newBut.type = 'button';
newBut.value = 'Say blah';
newBut.onclick = function(){aler t('blah')}
d.appendChild(n ewBut);
}

</script>

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

--
Rob
Dec 14 '05 #4
RobG wrote:
domas wrote:
spNew.appendChi ld(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.create Element('input' );
inpNew.type = 'button';
inp.onclick = function (){alert('OK')}
ip.id = 'btn';
spNew.appendChi ld(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 HTMLInputElemen t 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.addEventLis tener('click', functionObjectR eference, 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
3708
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? ============================ CODE ==============================================
2
3147
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 then the calender gone actually i want if i click outside off the calender then it should me removed ..How kan i do this ... Pls inform me as early as possible .. I am waiting for ur quick replay ...Here i attached the source code .... <!DOCTYPE...
0
8448
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8871
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8783
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8552
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7387
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6198
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2773
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.