Home  >  Article  >  Web Front-end  >  How to get the "selected value" and "selected text" of a standard drop-down box using JavaScript

How to get the "selected value" and "selected text" of a standard drop-down box using JavaScript

高洛峰
高洛峰Original
2016-11-25 15:01:44990browse

The standard drop-down box is very commonly used in HTML. There is often a need to get the text or value of the drop-down box when it changes.

Friends who are new to JS often mistake its value and text and confuse the two.

Sometimes it’s a mistake, because often our value is our text, but in fact they are different things.

Let’s give an example below.

We first create a drop-down box, as follows:

[html]


The JS code is as follows:

[javascript] function GetSelValue() {
    var objSel = document.getElementById("selOp");
                                                                 //This is to get the text
alert ("Current text: " + objSel.options(objSel.selectedIndex).text);
}
function GetSelValue() {
  var objSel = document.getElementById("selOp");
     
                                                             ("Current value: " + objSel.value);
" "

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn