CancelImage Upload

How to access a select box and its value via getElementByID on different browsers

Accessing the value of a HTML select box via getElementById() does not work the same way on all browsers. While the traditional getElementById value will work on Internet Explorer, Opera, and most other browsers, Netscape will not be able to interpret it and will break off the script mid function. Here then is what not to use:

//None Netscape:

document.getElementById("selectId").value

Thankfully there is a means by which the select box's value can be accessed across most modern day browsers, including Netscape. To do this though we will first have to find out which of the options is currently selected:

var i = document.getElementById("selectId").selectedIndex;

Then we can move on to access the value of that option, meaning the exact value of the select box:

document.getElementById("selectId").options[i].text;

The complete code (some people are to lazy after all to add up one and one and get two):

var i = document.getElementById("selectId").selectedIndex;

document.getElementById("selectId").options[i].text;


Login
Want to leave a comment?

No problem. Just enter your email and password below.


register | home | reminder

myDesignTool Networking • www.mydesigntool.cominfo@mydesigntool.com