wood burning stoves 2.0*
The moose likes HTML, CSS and JavaScript and the fly likes Dynamically adding a select control Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Dynamically adding a select control" Watch "Dynamically adding a select control" New topic
Author

Dynamically adding a select control

Vishnu Murthy
Ranch Hand

Joined: Jan 04, 2001
Posts: 56
My requirement is on click of a button i have to add <select> control with some options like <option>text<option>.
with following code i am able add a select control but i am not able add an <option> to it.
every time the button click i want to add a new <select> control.
my script is like
function AddSelect()
{
var tbody = document.getElementById("DefautlTable").getElementsByTagName("TBODY")[0];
var row = document.createElement("TR");
var td1 = document.createElement("TD");

td1.appendChild(document.createElement("<select name=select1>
<option selected><option>(-Select-)</option><option>htmlItem</option></select>"));

row.appendChild(td1)
tbody.appendChild(row);
}
would you help me in this aspect.
Vishnu


</script>
christoph weingarten
Ranch Hand

Joined: Oct 10, 2002
Posts: 39
Hi Vishnu,
you can use this function, to add a new option to a selectbox:
function addOption() {
// create a new option
myEntry = new Option('myText','myValue',false,true);
// the parameters for new options:
// 1. diplayed text for option
// 2. value for option (optional)
// 3. defaultSelected = true/false (optional)
// 4. selected = true/false (optional)
// paste it on the end of my selectbox
document.myform.mySelect.options[document.myform.mySelect.length] = myEntry;

}
This one should work. maybe there's a typo somewhere
regards
Christoph
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dynamically adding a select control
 
Similar Threads
Adding a row to a HTML table on-the-fly
dynamic table in java script
DOM and adding a brand new select list
How can I struts tag library with in javascript ?
Add a Table Row