Link to home
Start Free TrialLog in
Avatar of erzoolander
erzoolander

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of Alexandre Simões
Alexandre Simões
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of erzoolander
erzoolander

ASKER

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.

:)