Retrieving a select box value with javascript
A simple javascript makes for an easy way to grab the value from an HTML select input box. Need to access the value dynamically? Give the select box an id, and use the following script:
function getSelectValue(objId){
var selectObj = document.getElementById(objId);
return (selectObj.options[selectObj.selectedIndex].value);
}
Two lines and done. You can even do an all-in-one return statement like so:
return (document.getElementById(objId).options[document.getElementById(objId).selectedIndex].value)
Easy enough.
What exactly are we doing? Well, we’re indexing the select box’s options array with the array index of whichever option is selected, then grabbing the value associated with it. Quick and easy indeed.