views:

82

answers:

3
<select>
    <option>1</option>
    <option>2</option>
    <option class="test">3</option>
</select>

$('.test').attr('selected', 'selected');​

The select box above would choose the third option as default without any issues. However, if you check the elements with Firebug there isn't any selected="selected" attribute present within the chosen <option>.

I know this isn't a big issue as it still works but I need selected="selected" there so my other scripts could catch it and perform further processing. Are there any workarounds out there?

Thanks.

A: 

It should be $('.test').attr('selected', true);​

also

<option selected>value</option>

It will not show up as selected="selected"

Jakub
I don't think the value of 'selected' attribute matters(It need not be true, it could be any value). Just the fact that it is present is enough to select it.
letronje
@letronje: true, however I just followed the standard jQuery format `.attr( attributeName, value )`
Jakub
@Jakub: My point is that $('.test').attr('selected', 'selected'); also works ~> http://jsfiddle.net/MZYN7/1/
letronje
`$('.test').attr('selected', 'selected')` only works because when you assign a non-empty string like `'selected'` to a boolean property, it behaves like `true`.
bobince
@bobince thnx, didn't know that.
letronje
+2  A: 

This works as expected, Check it out : http://jsfiddle.net/MZYN7/1/

letronje
+4  A: 

The selected attribute does not correspond to the current selectedness state of the option. The selected attribute corresponds to the default selectedness, which will be restored if .reset() is called (or a type="reset" button is clicked) on the form. The current selectedness state can be accessed using the DOM property selected, whereas the default-selectedness, as reflected by the attribute, is accessed under the DOM property defaultSelected.

The same goes for value vs defaultValue on <input type="text">/<textarea>, and checked/defaultChecked on type="checkbox"/"radio".

jQuery's attr() is misleadingly named, and its attempts to pretend attributes and properties are the same thing is flawed. When you use attr(), you are usually accessing the property, not the attribute. Consequently, $(el).attr('selected', 'selected') is actually doing el.selected= 'selected'. This works because 'selected', as with any non-empty string, is ‘truthy’: it gets converted to el.selected= true. But this does not at any point touch the selected="selected" attribute.

IE further complicates this already confusing situation by (a) getting getAttribute/setAttribute wrong so it accesses the properties instead of the attributes (which is why you should never use these methods in an HTML document), and (b) incorrectly mapping the current form state to the HTML attributes, so the attributes do actually appear in that browser.

but I need selected="selected" there

Why? Are you serialising the form to innerHTML? This won't include form field values (again, except in IE due to the bug), so is not a usable way to store field values.

bobince
+1 for great response.
Jakub
Thanks. The other script _uses_ the current selected option(from DOM), or the default option if nothing has been selected yet for further processing. After reading your response, In order to let that script choose currently selected option, I guess the correct method would be setting the DOM property `selected` instead of using `selected="selected"`?
Yes. Though there are also limited conditions where some browsers will *also* set the current value when you set the default value. aaaargh!
bobince
Alright, actually I haven't messed with DOM properties before. For the sample above, I should use `$('.test').val(true);` to set the third option's `selected` DOM property to true? Is that correct? Thanks.
No, `val` accesses the value of the option, not its selectedness. In this specifc case, the `value=` attribute and the `value` DOM property *are* the same! (aargh!) You'd set the selectedness with the DOM property `selected`, so `$('.test').attr('selected', true)` (contrary to the name this is not setting an attribute!) or `$('.test')[0].selected= true`. Or, what's often more practical than futzing with `<option>` elements, set the value of the `<select>` directly, `$('select').val('3')`.
bobince