Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

jQuery: Check/uncheck a checkbox input or radio button

jQuery core : Exercise-5 with Solution

Check/uncheck a checkbox input or radio button.

Sample Data :

 <body>
 <form action="">
 <input type="radio" name="sex" value="male" checked>Male<br>
 <input type="radio" name="sex" value="female">Female
 </form>
 </body>

Solution:

HTML Code:

<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<meta charset="utf-8">
<title>Check/uncheck a checkbox input or radio button</title>
</head>
 <body>
 <form action="">
 <input type="radio" name="sex" value="male" checked>Male<br>
 <input type="radio" name="sex" value="female">Female
 </form>
 </body>
</html>

JavaScript Code :

$('input:radio[value=female]').prop("checked", true );;

Live Demo:

See the Pen jquery-core-exercise-5 by w3resource (@w3resource) on CodePen.


Contribute your code and comments through Disqus.

Previous: Find the specific option tag text value of a selected option.
Next: Write jQuery code to append a div element (and all of its contents) dynamically to the body element.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.