Programmatically Select Option

J

J1C

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 ...
 
L

Lee

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.
 
F

Fred Oz

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

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.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.
 
F

Fred Oz

Fred Oz wrote:
[...]
function selectOpt( f, s, v ){
var opts = document.forms[f].elements.options;
var o, i = opts.length;
while ( ( o == opts[--i] ) ) {


Agghh...

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

[...]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top