Home > Web Front-end > JS Tutorial > 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

高洛峰
Release: 2016-11-25 15:01:44
Original
1008 people have browsed it

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);
" "

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template