Is the .value property of the HTMLSelectElement property

Consider the html selection field with the identifier "MySelect".

Is it safe to get the value of the selected parameter as follows:

document.getElementById("MySelect").value;

instead of this:

var Sel = document.getElementById("MySelect");
var MyVal = Sel.option[MyVal.selectedIndex].value;

It seems safe, but I have never seen the documentation.

+5
source share
2 answers

It was not widely supported by older browsers.

If you want to be safe, you can use selectedIndexwith option, as you indicated in your question.

You can be sure to get value from below if you are worried.

document.nform.nselect.options[document.nform.nselect .selectedIndex].value

+2
source

Once upon a time, it was not safe for a cross browser. But these days I don’t know.

Using jQuery option? Because it:

$("#MySelect").val();

fully compatible with the browser.

+1

All Articles