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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Specifying Which Option Tag is Selected Within a Drop-Down Box

Status
Not open for further replies.

Krus1972

Programmer
Mar 18, 2004
145
US
If i have the following HTML code for a standard drop-down box:

Code:
<form name= "test" action="/default.asp" method="GET">
<select name="location">
	<option value="abilene">Abilene, TX</option>
	<option selected value="akron-canton">Akron / Canton</option>   
	<option value="albany-capital-region">Albany / Capital Region</option>
	<option value="albuquerque">Albuquerque</option>   
	<option value="allentown-reading">Allentown / Reading</option> 
	<option value="amarillo">Amarillo</option>
	<option value="anchorage">Anchorage</option> 
	<option value="ann-arbor">Ann Arbor</option>
	<option value="asheville">Asheville</option>  
	<option value="athens-ga">Atlanta</option>
	<option value="augusta">Augusta</option>  
	<option value="austin">Austin</option>   
	<option value="bakersfield">Bakersfield</option>
</select>

<input type="submit" value="Submit">
</form>

Notice how the second option value is "selected".

Does anyone know if there is a way, in Javascript, that will automatically change the "selected" option to whatever option that the user selected after the form is submitted?

The purpose is to always display the current, selected option, on the webpage, within the drop-down box.

Thanks for your help.
 
In Javascript you can change the selected status of an option element by making it true. i.e Or by making it the selectedIndex value of the dropdown.

selectObj.options{x].selected = true;
or
selectObj.selectedIndex = x;

Where selectObj is the object reference to the dropdown and x is the target item's index number. Index numbers start at 0 and are numbered in order as they appear in the html. So Abilene would be 0, Akron 1, Albuquerque 2 etc..







----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech
 
Thanks for your response.

Could you provide more detail? I am only an intermediate javascript programmer and I don't use it that often..

Thanks again for your help.
 
You want to display the selected options value on the forms action page?
When the form is submitted, the select object is gone. Maybe I don't understand what you want. When on the action page, ASP will read the form values you sent.

Lyndon
 
Could you provide more detail?

Like what exactly?

I've given you the general code to set a drop down item to be selected. You need to apply it to your situation. I cannot get any more specific with the amount of info you have provided.

What happens when your form is submitted? When exactly are you wanting to have the item be already be selected? Are you storing this selection somewhere? How does your page know what was selected previously by the user?

In other words, you need to be more specific about your workflow, before we can be any more specific about the solution.








----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top