Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use native JS to get the value and text values selected by the select element

Shulou Source: shulou.com Published: 2022-06-01 09:30:18 10月15日 Update

This article focuses on "how to use native JS to get the value and text values selected by select elements". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use native JS to get the value and text values selected by select elements.

The sample code is as follows (js is written directly in html, not in a separate external file):

The select element January February March April May June July August September October November December function showValue () {/ * gets the value*/ var select=document.querySelector ("# month") of the object selected by select Alert ("value:" + select.value);} function showText () {/ * get the text*/ var select=document.querySelector of the object selected by select ("# month"); alert ("text value:" + select.options [select.selectedIndex] .text);}

Interface:

The running effect is as follows:

Click to display the value

Click to display the text value

At this point, I believe you have a deeper understanding of "how to use native JS to get the value and text values selected by select elements". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Elements content objects learning practical deeper code interest practicality practical simple operation effects files methods more friends interfaces examples websites channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Xiaomi Shulou Information vpn Docker