Forum Moderators: open

Message Too Old, No Replies

preselect item in dropdown list box

         

babushka

3:49 pm on Sep 8, 2008 (gmt 0)

10+ Year Member



I am a javascript green pea. I can't get this code to work. Everything works except the list box doesn't show the item that is selected. I no the vars all all being filled and correct as I did alert boxes to look at the vars. And when the item is the one I want to select, it doesn execute the "new Option" with the "true". It just doesn't show it in the list box that it is selected. How do I make the selected option actually be visible in the drop down list box as the one selected if that makes sense?

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]);
}
}

}

Fotiman

5:12 pm on Sep 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm not totally sure I understand what you're trying to do but...

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?

babushka

6:36 pm on Sep 8, 2008 (gmt 0)

10+ Year Member



Thanks for your reply.

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?

babushka

6:38 pm on Sep 8, 2008 (gmt 0)

10+ Year Member



Thanks for your reply.

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?

babushka

6:41 pm on Sep 8, 2008 (gmt 0)

10+ Year Member



That extra '(' was a typo

Fotiman

8:27 pm on Sep 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You can set the selectedIndex value on the select box. Here's a complete example:

<!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>

babushka

9:32 pm on Sep 8, 2008 (gmt 0)

10+ Year Member



Thanks, that is exactly what I am doing. Works fine to set the elements but not for selecting the element. The 'true' is getting ignored.

Fotiman

3:53 am on Sep 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'll repeat myself...
You can set the selectedIndex value on the select box.

In my example, this was represented by this line:

fruit.selectedIndex = x;

In other words, not relying on the value specified in the call to new Option().