Meaningless Drivel is fun!
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 Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Dynamically adding a select control" Watch "Dynamically adding a select control" New topic

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>"));

would you help me in this aspect.

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
I agree. Here's the link:
subject: Dynamically adding a select control
It's not a secret anymore!