Dave's Brain

Browse - Programming Tips - How can I get the value of an HTML select in javaScript?

Date: 2009mar27
Language: javaScript

Q.  How can I get the value of an HTML <select> in javaScript?

A.  Use this function:

function getSelectValue(objSelect) { if (objSelect == null) return null; if (objSelect.selectedIndex < 0) return null; return objSelect.options[objSelect.selectedIndex].value; }
It works in all browsers.
function exampleUse() { var obj = document.getElementById('my_select'); var value = getSelectValue(obj); alert('value of my_select='+value); } // A generalized getValue() function... function getValue(obj) { var value; if (obj == null) return null; if (obj.options) { value = setSelectValue(obj); } else { value = obj.value; } return value; }

Add a comment

Sign in to add a comment
Copyright © 2008-2018, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.