jQuery <input> is checked via prop()

Introduction

The jQuery prop() method can get the checked property which specifies its checked or unchecked status.

View in separate window

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Check the Status of Checkboxes</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
  $(document).ready(function(){
        $('input[type="checkbox"]').click(function(){
            if($(this).prop("checked") == true){
                $("#result").html("Checkbox is checked.");
            }//from   w ww  .j  a  v  a  2 s.co  m
            else if($(this).prop("checked") == false){
                $("#result").html("Checkbox is unchecked.");
            }
        });
    });
</script>
</head>
<body>
  <p><input type="checkbox"> Check or uncheck the checkbox to get the status.</p>
    <div id="result" style="background: yellow;"></div>
</body>
</html>



PreviousNext

Related