How to get selected text from a drop-down list (select box) using jQuery?

jQueryWeb DevelopmentFront End Technology

With jQuery, it’s easy to get selected text from a drop-down list with :selected. This is done using the select id. You can try to run the following code to learn how to get selected text from a drop-down list using jQuery −

Example

Live Demo

<html>
   <head>
      <title>jQuery Selector</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
      <script>
         $(document).ready(function() {
            $("#submit").click(function(){      
               $("#myselection option:selected").text();
               alert( $("#myselection option:selected").text() );
            });
         });
      </script>
   </head>
   <body>
      <div>
         <p>The selected value:</p>
        <select id="myselection">
          <option value="1">First</option>
          <option value="2">Second</option>
        </select>
        <button id="submit">Result</button>
      </div>
   </body>
</html>    
raja
Published on 08-Dec-2017 15:35:56
Advertisements