jQuery <input> check or uncheck radio button dynamically

Introduction

Use the jQuery prop() method to check or uncheck radio button dynamically.

View in separate window

<!DOCTYPE html>
<html lang="en">
<head>
<title>jQuery Check/Uncheck Radio Button</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$(document).ready(function(){
    $(".check-male").click(function(){
        $("#male").prop("checked", true);
    });/*from w ww  . ja  v  a  2 s.  c o  m*/
    $(".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>



PreviousNext

Related