Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Programmatically Select Option

Reply
Thread Tools

Programmatically Select Option

 
 
J1C
Guest
Posts: n/a
 
      06-16-2005
If I only know the value of an option is a select list, is it possible
to programmatically select that option?

I would be returning the value from a server-side script ...

 
Reply With Quote
 
 
 
 
Lee
Guest
Posts: n/a
 
      06-16-2005
J1C said:
>
>If I only know the value of an option is a select list, is it possible
>to programmatically select that option?
>
>I would be returning the value from a server-side script ...


How are you returning it? If you're sending the whole page,
just give that option tag the SELECTED attribute.

 
Reply With Quote
 
 
 
 
Fred Oz
Guest
Posts: n/a
 
      06-16-2005
J1C wrote:
> If I only know the value of an option is a select list, is it possible
> to programmatically select that option?
>
> I would be returning the value from a server-side script ...
>


Loop through the options in the select. When you find one with the
required value, set its selected property to true.


function selectOpt( f, s, v ){
var opts = document.forms[f].elements[s].options;
var o, i = opts.length;
while ( ( o == opts[--i] ) ) {
if ( o.value == v ) {
o.selected = true;
return;
}
}
}

If it's a multiple select, drop the return.

--
Fred
 
Reply With Quote
 
Fred Oz
Guest
Posts: n/a
 
      06-16-2005
Fred Oz wrote:
[...]
>
> function selectOpt( f, s, v ){
> var opts = document.forms[f].elements[s].options;
> var o, i = opts.length;
> while ( ( o == opts[--i] ) ) {


Agghh...

while ( ( o = opts[--i] ) ) {
...

[...]

--
Fred
 
Reply With Quote
 
J1C
Guest
Posts: n/a
 
      06-22-2005
thanks all!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to programmatically select an option in jquery ui autocomplete Generale Cluster Javascript 1 05-31-2010 09:44 PM
programmatically select an option in "select" box in html rvelosoo Javascript 1 08-13-2008 10:53 AM
Removing select option removes entire select Nick Spacek Javascript 1 06-21-2006 04:45 PM
modify the default option in a select changing another select WebRaster Javascript 1 10-26-2005 10:36 AM
Selection from One SELECT changes selected option of another SELECT? J. Hall HTML 2 04-21-2004 05:36 PM



Advertisments