:selected Selector - Javascript jQuery Selector

Javascript examples for jQuery Selector:selected

Description

The :selected selector selects option elements that are pre-selected.

The following code shows how to select the pre-selected item(s) in a drop-down list:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $(":selected").css("background-color", "red");
});/*ww  w .j ava2s  .  com*/
</script>
</head>
<body>

<form action="">
Name: <input type="text" name="user"><br>
Car:
<select>
  <option>A</option>
  <option selected="selected">SSS</option>
  <option>B</option>
  <option>C</option>
</select>
</form>

</body>
</html>

Related Tutorials