3

I've seen the question "How does one determine selected option value in a HTML SELECT control" asked more times than I care to count - this and this and this for instance.

These questions all have something in common, they usually suggest one of the following methods:

//Method 1
document.getElementById("SelectControlsID").value;

OR

//Method 2
document.getElementById("SelectControlsID").options[document.getElementById("SelectControlsID").selectedIndex].value;

Question 1: I presume Method 2 is a relic from the past needed for some archaic browser that doesn't support calling .value directly? If so, which browsers still require the options[] method? Is this an ECMAScript spec issue?

Question 2: When writing new code, is it safe to just use Method 1 or is Method 2 still advisable (JQuery aside)?

Community
  • 1
  • 1
HeavenCore
  • 7,533
  • 6
  • 47
  • 62
  • It seems that this question may already be answered: http://stackoverflow.com/questions/15661298/do-select-elements-have-a-standard-value-property – David Thomas Nov 06 '13 at 12:02
  • 1
    @DavidThomas Well spotted! That question seems to suggest that .value is fine on IE6+ and that IE6 .value didn't work if there was no value tag on the option tag - interesting! – HeavenCore Nov 06 '13 at 12:17
  • To my mind, what it suggests is that .value (method 1) is fine if your select doesn't allow for multiple selections. If it does, use .options (method 2), but method 2 will work in both cases. – Agi Hammerthief Mar 06 '14 at 12:41

0 Answers0