Get <select> value in modern style?

I was impressed that to get the value from <select> you essentially had to do this:

 var sel = document.getElementById("my-select"); var val = sel.options[sel.selectedIndex].value; 

But I came across some code today that just does document.getElementById('my-select').value , which seems to work fine in Chrome and Firefox.

Has it changed recently or has it always been like that? How is this supported?

+6
source share
1 answer

mySelect.value been the W3C standard since at least October 1, 1998. See DOM Level 1 Specification . However, some IE browsers released after this date do not support it, including IE8 (I just tested it).

Edit: As @kennebec pointed out, the problem with IE8 is that it will not use the option text if there is no set value. If all your parameters matter, then mySelect.value will work in IE8.

+5
source

Source: https://habr.com/ru/post/943978/


All Articles