select an option

S

Sebastian Fey

hi,


how do you select an option using javascript?

i tried to setAttribute("selected", "disabled") to disable last
selected and setAttribute("selected", "selected") for the newly selected.

doesnt work :)

the last option in the list is always selected no matter which value
"selected" has.



greetings from Aachen,

Sebastian
 
G

GArlington

hi,

how do you select an option using javascript?

i tried to setAttribute("selected", "disabled") to disable last
selected and setAttribute("selected", "selected") for the newly selected.

doesnt work :)

the last option in the list is always selected no matter which value
"selected" has.

greetings from Aachen,

Sebastian

Try obj.getAttribute("selected") first. What do you get as a result of
this call?
Now, do you see why your code does not work?
 
R

RobG

hi,

how do you select an option using javascript?

i tried to setAttribute("selected", "disabled") to disable last

setAttribute sets the HTML selected attribute, not the DOM element's
selected property. Just don't use setAttribute, set properties
directly.

Also, the selected attribute is set to boolean true or false, not some
string value so use:

someOption.selected = false;

to "unselect" an option. If the select element's selectedIndex
property is set to -1, or all the options' selected properties are set
to false, no options will be selected.

selected and setAttribute("selected", "selected") for the newly selected.

doesnt work :)

That is equivalent to the HTML <option selected ... > which probably
has no effect on the currently selected option. Use:

someOtherOption.selected = true;

the last option in the list is always selected no matter which value
"selected" has.

You can either set the selectedIndex property of the select element,
or the selected property of a particular option element. There is no
need (in a single select) to set any other option's selected property
to false.

Things are a little different in a multiple-select.
 

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

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,195
Latest member
tewan

Latest Threads

Top