In several recent answers I see this code given as the way to retrieve the currently selected value of a <select> tag:
el.options[el.selectedIndex].value
Whereas the MDN documentation says that the .value property of an HTMLSelectElement is equivalent (“The value of this form control, that is, of the first selected option.“)
el.value
Is there some reason (old broken browsers?) why the longer version seems to be preferred?
To my best knowledge, the
.valueway has always worked.There is actually an extra problem with the first one – what happens when the user has not yet selected an option? Then the
.selectedIndexis -1, which will make your script crash.So go with
el.value