Reputation: 288390
Supposing a select like
<select name="myselect" id="myselect">
<option value="a">A</option>
<option value="b">B</option>
</select>
I can set the selected option changing it's value
property:
document.getElementById('myselect').value = 'b';
But I can't do it with value
attribute, so the following doesn't work:
<?php
$myselectValue = $_GET['myselect'] ?: 'a';
// Do something with $myselectValue
?>
<select name="myselect" id="myselect" value="<?php echo $myselectValue; ?>">
<option value="a">A</option>
<option value="b">B</option>
</select>
and, instead, I must write uglier and less clean server-side code.
I think it makes more sense to choose the selected option with <select>
's value
attribute instead of <option>
's selected
one.
Why did w3c think that <option value="x" selected>
is better than <select value="x">
? Maybe is there any advantage I don't see?
Upvotes: 3
Views: 512
Reputation: 191769
There's no telling what they were thinking, but there are a couple of cases where value
may not make sense:
<select multiple>
Also keep in mind that the property is different from the attribute.
Semantically it makes more sense to have the list of available options (through <option>
) and the select
attribute indicating the option (or options) that is actually selected rather than having to reconcile this with an attribute value.
Upvotes: 1