
Originally Posted by
nandoo
In my web page,I want to have a button(with name add).whenever i click that button it should generate a select list and also 3 text Fields.
This is very simple, but I'm confused about why you're doing some of the things evident in your code.
You create two tables and a div element. One table goes inside the other. The outer table is appended to the created div element, and that is added to an existing div. When you add the form controls, they are all inserted into one cell in the innermost table. Frankly, this structure makes absolutely no sense and results in a very badly constructed document tree. It would make more sense to have a single table for everything, and the controls are inserted into rows within that table.
I've produced code to do that in the past. An example already exists on the Web from a previous demonstration.
Whenever i select the list items the text fields should change accordingly.
How are they supposed to change. You don't provide any details. Is one particular input element supposed to take a value from the select element? Do they all change? Does it depend on the selected value?
var oSelect=document.createElement("select");
var oOption = document.createElement("OPTION");
var t = document.createTextNode("Ferrari");
oOption.setAttribute("value", 4);
oOption.appendChild(t);
You can, and should, just use the text and value properties when creating a option element within a HTML (but not a true XHTML) document. IE can be odd in its use of setAttribute, so it can be safer to sidestep that issue entirely.
oSelect.appendChild(oOption);
You cannot use appendChild to add an option element to a select element. IE will simply fail to do this properly. You must use the add method itself.
Code:
var s = document.createElement('select'),
o = document.createElement('option');
o.value = '4';
o.text = 'Ferrari';
s.add(o, null);
This is a very simplistic example which will not work in IE because Microsoft is a pain in the ass. It also doesn't contain any feature detection, which is vital when working with the DOM. I'll provide an alternative in a follow-up post, once you've address some of my earlier questions.
oSelect.onchange="txtChange()";
The intrinsic event properties expect a function, not a string. Though you provide a string when writing a listener in HTML, that code is converted internally into a function. You are required to do that yourself within a script. The solution is extremely simple though:
Code:
oSelect.onchange = txtChange;
Do not include parentheses () at the end as that will call the function. Here, we are just assigning a reference to that function.
document.getElementById("divide").innerHTML = document.getElementById("divide").innerHTML;
I really have no idea why you think that's necessary. If it actually does something useful (perhaps addressing the problem above), it's a truly horrible hack.
var di = document.getElementsByTagName("select");
This would retrieve every select element in the document as a collection (think: a simple array). I don't think that's a particularly good idea. The this operator will refer to the select element that fired the txtChange function:
Code:
oSelect.onchange = txtChange;
function txtChange() {
var selectElement = this;
}
The code you presented clearly needs work. However, you'll have to be very specific with what you want to achieve. Some people around here might be able to read minds. I can't.
Mike
Bookmarks