<html>
<body>
<p>Get Selected Value from Dropdown using JavaScript.</p>
<select id="select">
<option value="#ff0000">Red</option>
<option value="#09bd45">Green</option>
<option value="#3f00ff">Blue</option>
</select>
<script>
const selectE = document.querySelector('#select');
selectE.addEventListener('change', (event) => {
const selectedIndex = event.target.selectedIndex;
const options = event.target.options;
const selectedOption = options[selectedIndex];
const selectedText = selectedOption.text;
const selectedValue = selectedOption.value;
alert(`You selected: ${selectedText} (${selectedValue})`);
});
</script>
</body>
</html>
You may like these posts :