Forum Moderators: open
function ProcessTypes(selection){
var listBox = document.getElementById("ptype");
var chglistBox = document.getElementById("pcat");
if(selection != "") {
clearListBox("pcat");
selection = cleanString(selection);
for(var x=0;x<window[selection].length;x++){
if(pcatselected != "" && pcatselected == window[selection][x]) {
chglistBox[x+1] = new Option(window[selection][x],window[selection][x],true);
} else {
chglistBox[x+1] = new Option(window[selection][x],window[selection][x]);
}
}
}
Is chglistBox referencing a select element? if so, then you would add the new Option element like this:
chglistBox.options[x+1] = ...
In other words, you add new Option elements to the options collection of the select element, not to the select element itself. Does that help?
This works in setting the options for a dropdown list box:
listBox[x+1] = new Option("something", "something");
But, I want to specify which one is 'selected'. I thought I could do this: (Just add 'true' to the end)
listBox[x+1] = new Option(("something", "something", true);
But that doesn't work.
How else can I programmatically select an option in a dropdown listbox?
This works in setting the options for a dropdown list box:
listBox[x+1] = new Option("something", "something");
But, I want to specify which one is 'selected'. I thought I could do this: (Just add 'true' to the end)
listBox[x+1] = new Option(("something", "something", true);
But that doesn't work.
How else can I programmatically select an option in a dropdown listbox?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" >
<title>Untitled</title>
</head>
<body>
<select id="fruit">
<option value="apple">Apple</option>
</select>
<script type="text/javascript">
window.onload = function() {
var fruit = document.getElementById('fruit');
var x = fruit.options.length;
fruit.options[x] = new Option('Orange','orange',true);
fruit.selectedIndex = x;
}
</script>
</body>
</html>