Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dynamically adding a select control

 
Vishnu Murthy
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic