Menu Close

Jquery :selected selector with example

Selected selector

Here, we will learn about :selected selector in jquery. The :selected selector is used to select the pre-selected item(s) in a drop-down list.

Syntax:

$(":selected")

Follow the below example to learn about :selected selector in jquery.

Example:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Jquery :selected selector</title>
    <meta charset="utf-8">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script> 
        $(document).ready(function(){
             $(":selected").css("background-color", "#009933");
        }); 
    </script>
    <style>
        .select-style {
            padding:50px;
            text-align: left;
        }
    </style>
</head>
<body>
    <div class="select-style">
        Name: <input type="text" name="user"><br><br>
        <select>
            <option>Vegetables</option>
            <option selected="selected">Fruits</option>
            <option>Milk</option>
            <option>Snacks</option>
        </select>
    </div>
</body>
</html> 
Posted in jQuery, Web Technologies

You can also read...