Re: Selecting a SELECT value BY value?
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: Selecting a SELECT value BY value?

From: <@sh>
Date: Wed Mar 22 2006 - 13:17:35 CET

Thanks for your reply, I think I understand what you mean, although I'm
trying to set the selected option in the SELECT via an onClick from another
element on the page? I think you've misunderstood my question?

"Erwin Moller"
<since_humans_read_this_I_am_spammed_too_much@spamyourself.com> wrote in
message news:442133ca$0$11064$e4fe514c@news.xs4all.nl...
> @sh wrote:
>
>
> Hi
>
>> Probably very simple, but I have a SELECT box The value of each option in
>> the Select box will be a number, for instance...
>>
>> <option value="5">A test entry</option>
>>
>> Therefore, how do I, via an onClick, change the selected option of a
>> Select via the Value of the select only? I can't use the 'selectedIndex'
>> because it doesn't relate to the value of the option.
>
> Yes it does relate to the value.
> It is actually POINTING to the selected index, which is more usefull than
> the value, since the same value can be found for different options!
> So use it to point to the right OPTION in the selectbox, and retrieve the
> value or text.
>
> Example:
>
> <form name="fakeform">
> <select name="example" onClick="getVal();">
> <option value="1">One
> <option value="2">Two
> <option value="3">Three
> <option value="1">Also One
> </select>
> </form>
>
> <script type="text/javascript">
> function getVal(){
> // get the index of the selected one
> var selInd = document.forms["fakeform"].example.selectedIndex;
> var correspondingValue =
> document.forms["fakeform"].example[selInd].value;
> var correspondingText = document.forms["fakeform"].example[selInd].text;
> // do something usefull with it
>
> alert("selInd="+selInd+"\ncorrespondingValue="+correspondingValue+"\ncorrespondingText="+correspondingText);
> }
> </script>
>
> Regards,
> Erwin Moller
>
>
>>
>> Thanks!
>
Received on Mon May 1 04:19:49 2006