Click to See Complete Forum and Search --> : innerHTML with SELECT option's
gnanesh
08-08-2003, 04:22 PM
If any one knows how to insert <option> tags between <SELECT></SELECT> using
document.getElementById("").innerHTML = '<option></option>';
if i give Id to the <select id="aa"> its not working
Let me know
Regards
Gnanesh
David Harrison
08-08-2003, 04:42 PM
You could try this:
new_thing=document.createElement("option");
new_thing.value="Hi";
But, where in normal HTML you can do this:
<option value="Hi">Ed TV</option>
and Ed TV will appear in the dropdown menu, I don't know how you can make anything appear in the menu with js.
Try this:
<script type="text/javascript">
function insertOption() {
anOption = document.createElement("option")
document.myform.time.options.add(anOption, 0)
anOption.innerText = "Zero"; //The text for the option
anOption.Value = "Zero"; //The option's value
document.myform.time.options.selectedIndex = 0;
}
</script>
</head>
<body>
<form name="myform">
<select name="time" size="1">
<option value="One">One</option>
<option value="Two">Two</option>
<option value="Three">Three</option>
</select>
<input type="button" name="now" value="Insert" onclick="insertOption();">
</form>
David Harrison
08-08-2003, 05:48 PM
Aha, so it's innerText is it. I'll make a mental note of that.
Khalid Ali
08-08-2003, 06:31 PM
Originally posted by lavalamp
Aha, so it's innerText is it. I'll make a mental note of that.
No its not innerText
The proper way to add text to an element will be ( if you take the example above)
this line
anOption.innerText = "Zero"; //The text for the option
should be
anOption.appendChild(document.createTextNode("Zero"));