उदाहरण
onchange इवेंट और selectedIndex से सिलेक्टेड ऑप्शन का टेक्स्ट प्राप्त करें।
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<select onchange="selectedSubjectName()" id="subjectName">
<option>Javascript</option>
<option>MySQL</option>
<option>MongoDB</option>
<option>Java</option>
</select>
<script>
function selectedSubjectName() {
var subjectIdNode = document.getElementById('subjectName');
var value = subjectIdNode.options[subjectIdNode.selectedIndex].text;
console.log("The selected value=" + value);
}
</script>
</body>
</html>
फ़ाइल सेव करें और Live Server से खोलें।
आउटपुट
ड्रॉपडाउन बदलने पर कंसोल में सिलेक्टेड वैल्यू दिखाई देगी।