How do I get the value from a combo in Javascript?

If you don’t use any javascript framework such as jquery.js or prototype.js the combo object isn’t so easy to use,…

Novembre 13, 2009

If you don’t use any javascript framework such as jquery.js or prototype.js the combo object isn’t so easy to use, because when you try to get the selected value or the selected option string you can’t do it simply with obj.value as you do for a input type text box.
You can add this two function to the object:

HTMLSelectElement.prototype.getValue = function () {
	return this.options[ this.options.selectedIndex ].value ;
}
HTMLSelectElement.prototype.getText = function () {
	return this.options[ this.options.selectedIndex ].innerHTML ;
}

So you can reach the value and the option string simply this way:

var SelectedValue = document.form.comboname.getValue();
var SelectedOptionString = document.form.comboname.getText();

Author

PHP expert. Wordpress plugin and theme developer. Father, Maker, Arduino and ESP8266 enthusiast.

Recommended

Click links with JavaScript

If you want to click on objects, anchors, table cells or any other html element to emulate the javascript behaviour…

Novembre 18, 2009

The Quantcast CMP broke my sites

A javascript error in CMP blocks my site.

Maggio 19, 2023

Scroll to DIV by ID without jQuery

Use scrollIntoView instead of jQuery animate.

Dicembre 16, 2022

React links

Collection of links and notes while approaching React.js, next.js and related topics.

Dicembre 14, 2022

Add Internet Explorer class to body to detect old browser

When you have clients that still uses an old browser, you need to handle it. This javascript function detects the…

Febbraio 27, 2017