How to display the selected option in a dropdown list with JavaScript?

HTMLJavascriptProgramming Scripts

To display the selected option in a dropdown list with JavaScript, you can try to run the following code. This allows the user to get the value he selected from the dropdown list.

Example

Live Demo

<!DOCTYPE html>
<html>
   <body>
      <form id="myForm">
         <select id="selectNow">
            <option>One</option>
            <option>Two</option>
            <option>Three</option>
         </select>
         <input type="button" onclick="display()" value="Click">
      </form>
      <p>Select and click the button</p>
      <script>
         function display() {
            var obj = document.getElementById("selectNow");
            document.write(obj.options[obj.selectedIndex].text);
         }
      </script>
   </body>
</html>
raja
Published on 26-Feb-2018 13:27:20
Advertisements