jquery - Select options without value attribute -
is correct or invalid markup (when option has no value)?
expected jquery behaviour .val()
when there no value=""
assigned?
<select id="select_id"> <option></option> <option>name</option> <option>recent</option> </select>
if use $("#select_id").val();
"name"/"recent", if option had value, value's content.
i know there .text()
"name" , "recent" in example. confused why .val()
gives me .text()
when there no value assigned.
fiddle (notice different result of alerts)
this has nothing jquery. that's how browser handles it.
$("#select_id").change(function () { var val = $("#select_id option:selected")[0].value; alert(val); // somevalue or recent });
reference: http://www.w3.org/tr/html5/forms.html#attr-option-value
the
value
attribute provides value element. value ofoption
element value ofvalue
content attribute, if there one, or, if there not, value of element'stext
idl attribute.
Comments
Post a Comment