Given the HTML:
<select id="mySelect">
<option value="1">test1</option>
<option value="2">test2</option>
<option value="3">test3</option>
</select>
and the javascript:
var e = document.getElementById('mySelect');
To get the value of the select I can use e.value
and e.options[e.selectedIndex].value
.
I am aware that e.options[e.selectedIndex].value
will give me the selected value (1,2 or 3) and e.options[e.selectedIndex].text
would give me test1, test2, test3 depending on which is selected.
Is it ok to use just e.value
? was this a problem in old browsers?
which is more correct: e.value
vs e.options[e.selectedIndex].value
?
e.value
. There're no browsers compatibility issues documented: developer.mozilla.org/en-US/docs/Web/HTML/Element/select – Clovae.value
takes less space which can be desirable from a bandwidth point of view. – Descombes.value
on the select itself, you had to get the selected option and take the value of that. – Chaoanvalue
is not an attribute ofselect
according to MDN or W3C (w3.org/wiki/HTML/Elements/select). However, it seems to work in all browsers, so looks like it's safe to use. – Iridectomy.value
– Boxhauloptions ... selectedIndex
: #1086301 – Iridectomy