selected selector in jQuery with example



selected selector returns all selected input elements.


Example :



    <html>
    <head>
    <title>selected Selector</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"> </script>
    <script type="text/javascript">
    $(document).ready(function()
    {
    $("#myButton").click(function()
    {
    $("select option:selected").css("color", "Red");
    });
    });
    </script>
    </head>
    <body>
    <select name="players" multiple="multiple">
    <option>Henry</option>
    <option selected="selected">Messi</option>
    <option>Fabregas</option>
    <option selected="selected">Luiz</option>
    <option>Modric</option>
    <option>Wilshere</option>
    </select>
    <button id="myButton">Click</button>
    <button onclick="location.reload()">Reset</button>
    </body>
    </html>
    

Demo :





In the above example, we have used selected selector. Selected selector returns all selected input elements. In above example, it selects the selected options and applies CSS to it.

0 comments:

Post a Comment