value of select elements with "e.value" vs "e.options[e.selectedIndex].value"
Asked Answered
T

3

5

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?

jsFiddle

Tomasz answered 11/8, 2014 at 18:26 Comment(12)
I can't see why not use the standard e.value. There're no browsers compatibility issues documented: developer.mozilla.org/en-US/docs/Web/HTML/Element/selectClova
Both are correct. e.value takes less space which can be desirable from a bandwidth point of view.Descombes
@Tomasz As you have suggested, it was an issue in very old browsers. You could not use .value on the select itself, you had to get the selected option and take the value of that.Chaoan
@JamesMontagne ok, interesting. Which browsers in IE-time-measures had problems? IE6- or less old like IE8-?Tomasz
value is not an attribute of select 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
@Tomasz A quick google found that it didn't work at all in Netscape 4 and that IE6&7 had some oddities, though it wasn't fully broken.Chaoan
@RickHitchcock interesting also. Hmmm... if W3C does not refer it feels strange this works. I would like to see a answer explaining my question.Tomasz
@RickHitchcock: you need to learn about the difference between DOM properties and HTML attributes. Select elements do indeed have a .valueBoxhaul
@MelanciaUK: The incompatibilities not being documented doesn't mean they don't exist :-) MDN isn't a very reliable ressource in that regard.Boxhaul
@Bergi, I stand corrected! Nice to know select.value is safe to use. But is that new as of HTML5? All the discussion here says we should use options ... selectedIndex: #1086301Iridectomy
So many of us have been using selectedIndex for so many years - not because value wasn't a standard property, but because IE6 didn't support it. It's safe to say IE6 isn't an issue anymore. I have to wonder what other convolutions I use in my code, simply because IE6 didn't follow standards. Sorry for the confusion!Iridectomy
@RickHitchcock you are so right. IE not following standards has been "painfull".Tomasz
L
6

The HTMLSelectElement interface includes the value attribute at least since Document Object Model (DOM) Level 1 Specification, from 1998.

However, like it is explained in this w3c mailing list, the problem was that HTML4.01 spec was vague:

It's true that HTML4.01 doesn't explicitly specify a value attribute for SELECT, but it does seem to be implied:

  • 'Menu' is a control type. (HTML4.01 17.2.1)

  • "Each control has both an initial value and a current value, both of which are character strings" (HTML4.01 17.2)

  • And SELECT may have an onchange attribute which implies a value. (HTML4.01 17.6)

But there's no mention of what the value represents, nor of what the initial or default values might be.

However, checking in IE5 and Mozilla, the value of SELECT does indeed return a string corresponding to the value of the currently selected OPTION.

(...) Probably wouldn't be a problem if HTML4.01 had been more explicit.

This was fixed in following definitions.

You can see it defined here:

So I think it's safe to use.

Lunkhead answered 12/8, 2014 at 15:57 Comment(0)
B
3

Some old (~2005) threads from the comp.lang.javascript newsgroup, as well as their FAQ [1], mention that .value access was not supported in Netscape Navigator 4 (i.e. pre-2000), and some other mobile and desktop browsers that were considered "old" even at that time.

Conclusion (backed by @Oriol's DOM spec excerpts): It's totally safe to use today.

Boxhaul answered 13/8, 2014 at 1:6 Comment(0)
B
0
document.getElementById('submit').onclick = function() {
    var e = document.getElementById("pets");
    var text = e.options[e.selectedIndex].text;
    document.getElementById("container").innerHTML = 'The selected text is ' + text;
}
Bowne answered 12/1, 2022 at 14:1 Comment(1)
While this code snippet may solve the problem, it doesn't explain why or how it answers the question. Please include an explanation for your code, as that really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.Carn

© 2022 - 2024 — McMap. All rights reserved.