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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/jquery-exercises/jquery-core-exercise-5.php