INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

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

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

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

(OP)
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.

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

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

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

(OP)
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.

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

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

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

Quote:


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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close