Source Code:
(back to article)
Submit
Result:
Report an issue
<!DOCTYPE html> <html> <head> <title>Title of the Document</title> <script src="https://code.jquery.com/jquery-3.5.0.min.js"></script> </head> <body> <select id="my-select"> <option value="option1">Option 1</option> <option value="option2">Option 2</option> <option value="option3">Option 3</option> </select> <script> var selectBox = document.getElementById("my-select"); var selectedOption = selectBox.options[selectBox.selectedIndex]; var selectedText = selectedOption.textContent; alert(selectedText); // alerts the text of the selected option to the console </script> </body> </html>