jQuery :checked Selector


The :checked selector in jQuery is used to look for all the checked radio button or checkboxes and selects them.

Syntax

The syntax is as follows −

$(":checked")

Example

Let us now implement the :checked selector in jQuery −

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<h2>Favourite Subject</h2>
<form>
<div>
Maths: <input type="radio" name="sub" value="Maths">
</div>
<div>
English <input type="radio" name="sub" value="English">
</div><br>
<div id="content"></div>
</form>
<script>
   $( "input" ).on( "click", function() {
      $( "#content" ).html( $( "input:checked" ).val() + " is the favourite subject" );
   });
</script>
</body>
</html>

Output

This will produce the following output −

Select “Maths” radio button above −

Now, select “English” radio button −

Updated on: 05-Nov-2019

233 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements