Hi Devloper, in this tutorial we will learn how to check checkbox is checked or not using jquery, This article, we will create a demo code in which we will use the jquery click event to check if the checkbox is checked or not.
We use the jquery click () event and when we click the checkbox the function will run and check if the checkbox is checked or not, follow my example right now.
Jquery Code :
$("body").on("click", ".i_agree", function(event){
if($(this).prop("checked") == true){
alert("checkbox is a checked");
}else if($(this).prop("checked") == false){
alert("checkbox is a unchecked");
}
});
Example :
<!DOCTYPE html>
<html>
<head>
<title>Jquery check checkbox is checked or not example - phpicoder.com</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<h1>Jquery check checkbox is checked or not example - phpicoder.com</h1>
<label>
<input type="checkbox" name="i_agree" class="i_agree"> I agree
</label>
<script>
$(document).ready(function(){
$("body").on("click", ".i_agree", function(event){
if($(this).prop("checked") == true){
alert("checkbox is a checked");
}else if($(this).prop("checked") == false){
alert("checkbox is a unchecked");
}
});
});
</script>
</body>
</html>
I hope it can help you...
Related Post