PDA

View Full Version : Set listMenu Selection With Javascript?


robocop
07-28-2003, 03:12 PM
can javascript set the selection of a drop down box?

like if i clicked on a picture of the "teddy bear pattern" in a popup window of all the available patterns....the list box on the page that spawned the pop up would swich to "teddy bears"...

is this possible? im diggin in my DHTML Reference....and nothing that i can find....

thanks yall....

kasey

beetle
07-28-2003, 03:26 PM
<img onclick="opener.forms['formName'].elements['elementName'].selectedIndex = someInteger;"/>

Replace the italics with your values

robocop
07-28-2003, 03:34 PM
awesome! is there any way to select the box by the Value as opposed to the option number? cause there could be new items added....

whats the best way to populate a list box from a database?...i think that may be the best route if these are going to be changing all the time...perhaps i should look into that....

beetle
07-28-2003, 03:51 PM
You'd need to created a function that "searched" the select element's options for a value

function selectByValue( elem, value )
{
var opt, i = 0;
while( opt = elem.options[i++] )
{
if ( opt.value == value )
{
opt.selected = true;
return;
}
}
}

robocop
07-28-2003, 03:55 PM
damn man...you got some game...ill letcha know how this worked....