Reputation: 2001
enter code here
i am using the folllowing code . I wnat that the only single option is selected. but right now its showing some other options as selected by defaul when the page loads.
How can i make one particular of my choice o selected?
<select name="ms"> <option value="-1" selected="false" >any</option>
<option value="0" selected="true" >only single</option>
<option value="1" selected="false" >only married</option>
</select>
Upvotes: 4
Views: 5331
Reputation: 784
HTML4 + HTML5 : use attribute minimization; therefore use the attribute 'selected' for the selected option (no attribute for the others)
<select name="ms"> <option value="-1" selected="false" >any</option>
<option value="0" selected>only single</option>
<option value="1">only married</option>
</select>
XHTML : attribute minimization is forbidden, meaning you need to assign a value to the attribute, i.e. selected="selected" (which is the only value it takes)
<select name="ms"> <option value="-1" selected="false" >any</option>
<option value="0" selected="selected">only single</option>
<option value="1">only married</option>
</select>
Check the DOCTYPE of your html page/file to see if you use XHTML or HTML.
Upvotes: 1
Reputation:
<select name="ms">
<option value="-1" selected="false" >any</option>
<option value="0" selected="true" >only single</option>
<option value="1">only married</option>
</select>
Upvotes: 0
Reputation: 12535
<option value="0" selected="selected" >only single</option>
<option value="1" >only married</option>
Upvotes: 0
Reputation: 44349
Browsers generally only check if the selected attribute exists. Therefore you should change your code to:
<select name="ms">
<option value="-1">any</option>
<option value="0" selected="selected">only single</option>
<option value="1">only married</option>
</select>
EDIT: Looks like you edited your example so I'll edit mine.
Upvotes: 1
Reputation: 22719
Don't supply the "selected" attribute at all, if the option isn't selected. This will work better:
<select name="ms">
<option value="-1" >any</option>
<option value="0" selected >only single</option>
<option value="1" >only married</option>
</select>
Upvotes: 0
Reputation: 31781
The selected attribute's presence alone is enough to make the option selected. You will need to remove the selected="false"
text from the second option to make this work. selected and disabled are similar in this respect.
Upvotes: 2