Re: select an item on a dropdown list
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.javascript archive

Re: select an item on a dropdown list

From: ASM <stephanemoriaux.NoAdmin@wanadoo.fr.invalid>
Date: Wed Jul 27 2005 - 20:05:17 CEST

passion_to_be_free@hotmail.com wrote:
> This is probably simple, but I can't seem to find it anywhere.
>
> I have have some values stored in javascript variables. I have a
> <select> dropdown list whose options correspond to these values. I want
> to be able to select an item on the dropdown list based on the value of
> the javascript variable.
>
> Let's say this is my list and my variable:
>
> <select id='popup'>
> <option value="default">--Please Choose a saying--</option>
> <option value="hello">Hello</option>
> <option value="goodbye">Goodbye</option>
> </select>
>
> var input = "hello";
>
> Is there a way to select the 2nd option using using that variable
> value? Something like:
>
> var popup = document.getElementById("popup");
> popup.selectedItem = input;

to get the selected option value :
  alert('popup choice = '+popup.options[popup.selectedIndex].value);

to get the selected option item :
  alert('popup choice = '+popup.options[popup.selectedIndex].text);

with 'hello', to show the option in select :
  for(var i=0;i<popup.length;i)
     if(popup[i].value=='hello') popup.selectedIndex = i;

other usefull :

<select id='popup' onchange=" var k = this.selectedIndex;
   if(i==0) alert('Do other choice');
   else
   alert('choice = '+this.options[k].value);">

-- 
Stephane Moriaux et son [moins] vieux Mac
Received on Tue Oct 18 03:02:24 2005