سؤال

UPDATE

Unfortunately I have caused some confusion by talking about the .value property, but then asking for any reference to feature support in browsers.

In hindsight, I guess the thing I needed right now was to know whether .value is "safe" to use, and therefore that is why I accepted @BeatAlex's answer (as they put the effort in to actually test on multiple browser.


ORIGINAL QUESTION

Using javascript, the accepted way to get/set the value of the selected <option> in a <select> is using the .value property.

For years and years I have not used the .value property, as I was told that "old browsers" don't support it. Instead I use the long form of...

dd.options[dd.selectedIndex].value;

But I've just done some research, and I cannot find any reference to which "old browsers" this effects. For instance this quirksmode article even mentions "old browsers" but doesn't give any more information than that.

Which "old browsers" do not have the .value property on the <select> element? Is there a reference somewhere to exactly when particular features became available in mainstream browsers?

Note: unfortunately jQuery is not currently available to me, due to an old 3rd party component being used on the system

هل كانت مفيدة؟

المحلول

.value works for me in most oldest browsers supported in windows XP.

<select id="select">
  <option value="Hello1">1</option>
  <option value="Hello2">2</option>
  <option value="Hello3">3</option>
  <option value="Hello4">4</option>
  <option value="Hello5">5</option>
</select>

JavaScript:

var id = document.getElementById("select");

id.onchange = function(){
 alert(this.value);
}

This works on:

(All run from Windows XP)

  • IE6

  • Firefox 3.0

  • Safari 4.0

  • Chrome 14.0

  • Opera 10.6

This is as far as Browserstack goes back to.

نصائح أخرى

It seems that we need to go down to IE 3 to find a browser that does not support the value property for a select element. I found a problem description saying: “I'm having some trouble getting the value of a selected option in IE3.0.2. The following [code that accesses the value property] works in all flavors of Netscape and IE 4” and quoting the IE 3 error message “Value is not an object”.

This means in practice that we can now regard value as universally supported.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top