Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change selection

Status
Not open for further replies.

elliottmina

Programmer
Feb 6, 2005
12
US
I have having trouble changing the selected option in a <select> box. From reading, I thought the correct code would be:

document.formname.fieldname[index].selected=true;

but it's not working. Is this the correct format? TIA.
 
Form name = form1
Select = mySelect
Both of these will set the control to the second item in the list.
document.form1.mySelect.selectedIndex=1;
document.form1.mySelect[1].selected=true;
 
document.getElementById("theid").selected=true;

document.formname.fieldname.selected=true;

JavaScript Beginner at work :)
 
Thank you very much. The darn thing still isn't working, but I'll keep at it.
 
This get the selected value:

document.formname.selectname.selectedIndex

and if you want the value of the selected:

document.formname.selectname.options[document.formname.selectname.selectedIndex].value

staffa
 
Please remember that the index is zero-based - i.e. the first list element is numbered 0, not 1. Other than that, if nothing happens when you run some code containing the examples given above (including your own), I'd suggest that there's probably a typo in your code leading up to that point.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top