JQuery Selector selected

From CoderLearner
Jump to: navigation, search

Description

Select all elements that are selected for the HTML SELECT element.

Syntax

$(':selected')

$('select[name="value"] option:selected')

Example 1

<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Select</title>
      <script 
         type="text/javascript" 
         src="/lib/jquery-1.7.2.min.js">				 
      </script>

      <script>
         $().ready(function() {
            $('select').change(function(){
               var str= $('select[name="major"] option:selected').text() ;
               
               $('span').text(str);
            })
         });
      </script>
   </head>
   <body> 
      <span></span>
      <form>
         <select name="major">
            <option value="computer science">Computer Science</option>
            <option value="English">English</option>
            <option value="Business">Business</option>
            <option value="Sport">Sport</option>
            <option value="Maths">Maths</option>
         </select><br/>
      </form>
   </body>
</html>

Try It