views:

255

answers:

6

enter code herei 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>
+2  A: 

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.

David Andres
A: 

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>
John Fisher
You should use selected="selected" though, as that is actually more correct.
James Black
+1  A: 

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.

Eric Wendelin
A: 
<option value="0" selected="selected" >only single</option>
            <option value="1" >only married</option>
JayJay
A: 

As Eric stated use the selected attribute:

W3C Recommendation:

The initial state has the first option selected, unless a SELECTED attribute is present on any of the elements.

<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>
rick schott
A: 
<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>