JQuery/Javascript Set Option Value to Selected based upon POST value

I'm more of a backend/PHP guy - with enough JS knowledge to be dangerous - so excuse this if it's a dumb question.

I've got an select list with several hundred possibilities in it...
<select name="city">
                                <option value="_none">- None -</option>
                                <option value="577">Addison [Texas]</option>
                                <option value="595">Agana [Guam]</option>
                                <option value="224">Airport Hotels [No State]</option>
                                <option value="162">Akron [Ohio]</option>
                                <option value="152">Albany [New York]</option>
                                <option value="359">Albuquerque [New Mexico]</option>
                                <option value="513">Alexandria [Virginia]</option>
                                <option value="352">Allen Park [Michigan]</option>
                                <option value="286">Amish Country [Pennsylvania]</option>

Open in new window


When the user POSTS to the page - I'd like to ensure that the one selected appends "selected" to the option value tag.

So - if they selected "359" in the search query - I'd like to have the script take:

<option value="359">Albuquerque [New Mexico]</option>

and append "selected" to it...so it reads:

<option value="359" selected>Albuquerque [New Mexico]</option>

...If this were a backend generated list - I'd know how to do it - but since it's hard coded into the HTML and not dynamically generated - I figure it has to be done via JS on the front end.  Since there are several hundred possibilities - doing a bunch of conditionals is impractical...so I figure a jQuery/JS function is probably best.

Any help would be appreciated :)

E
LVL 2
erzoolanderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alexandre SimõesManager / Technology SpecialistCommented:
Here's a working sample: http://jsfiddle.net/AlexCode/vgtvafg8/

Just add an ID to the element because it's the fastest way to find it.
I'm not sure where you get the value to set on the select. On my example I'm hard-coding it on the function call.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Albert Van HalenAnalyst developerCommented:
You can't access posted data on the client from the request...
However, you can access it serverside.

So you need to render a little javascript statement server side in order for the client to execute.
<script>document.getElementById('city').value = '359';</script>

Open in new window

You need to specify the id attribute on the selectbox in order to make this work.

If you don't specifiy the id on the element, you can use getElementsByName method, but that will return a list of elements since the name of an element is not necessarily unique.
0
erzoolanderAuthor Commented:
I'm also doing the PHP - so basically I just took the POST that way and chucked it into a javascript variable -

var searchcity = <?php echo($_POST['searchcity']);?> and then called the function.

:)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.