jQuery Selector :selected

Introduction

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

$(":selected")

Select the pre-selected item(s) in a drop-down list:

View in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $(":selected").css("background-color", "red");
});/*from  w w w . j  av a  2s  .  c om*/
</script>
</head>
<body>

<form action="">
Name: <input type="text" name="user"><br>
Car:
<select>
  <option>CSS</option>
  <option selected="selected">HTML</option>
  <option>Mercedes</option>
  <option>Audi</option>
</select>
</form>

</body>
</html>



PreviousNext

Related