Q:

How to check or uncheck radio button dynamically using jQuery

0

How to check or uncheck radio button dynamically using jQuery

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Use the jQuery prop() method

You can use the jQuery prop() method to check or uncheck radio button dynamically such as on click of button or an hyperlink etc. The prop() method require jQuery 1.6 and above.

Let's check out the following example to understand how it basically works:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Check/Uncheck Radio Button</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
    $(".check-male").click(function(){
        $("#male").prop("checked", true);
    });
    $(".check-female").click(function(){
        $("#female").prop("checked", true);
    });
});
</script>
</head>
<body>
    <label><input type="radio" name="gender" id="male"> Male</label>
    <label><input type="radio" name="gender" id="female"> Female</label>
    <button type="button" class="check-male">I'm Male</button>
    <button type="button" class="check-female">I'm Female</button>  
</body>
</html>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

JavaScript / jQuery Frequently Asked Questions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to remove first character from a string in jQu... >>
<< How to disable all input controls inside a form us...