Reputation: 4419
I am trying to have a drop down option pre-selected based on a string that I have on the server side. For example I have the following options:
<select name="postsport">
<option value="choose">Choose a sport</option>
<option value="general sea">--general sea--</option>
<option value="sailing">sailing</option>
<option value="diving">diving</option>
<option value="sailing">surfing</option>
<option value="kiteboarding">kite boarding</option>
<option value="kayaking">kayaking</option>
<option value="general land">--general land--</option>
<option value="rockclimbing">rock climbing</option>
<option value="hiking">hiking</option>
</select>
And on the serverside I know that the pre-chosen option is "sailing" is there any way with javascript or jquery or some other method to have this option prechosen and out on the front?
Upvotes: 0
Views: 993
Reputation: 15143
When you generate the HTML just modified the default option to say
<option value="sailing" selected="selected">sailing</option>
Leave everything else as is.
Upvotes: 1
Reputation: 7735
If you want do it via js:
$('option[value=sailing]').attr('selected','selected')
Upvotes: 2
Reputation: 5283
Use this
<?php if($selected){
echo "<option value=\"choose\" selected=\"selected\">Choose a sport</option>";}
?>
Upvotes: 0