1Answer
Use the jQuery prop()
method & :checked
selector
The following section describes how to track the status of checkboxes whether it is checked or not using the jQuery prop()
method and :checked
selector.
Using the jQuery prop()
Method
The jQuery prop()
method provides an simple and reliable way to track down the status of checkboxes. It works well in all condition because every checkbox has checked property which specifies its checked or unchecked status.
Do not misunderstand it with the checked
attribute. The checked
attribute only define the initial state of the checkbox, and not the current state.
<script type="text/javascript">
$(document).ready(function(){
$('input[type="checkbox"]').click(function(){
if($(this).prop("checked") == true){
alert("Checkbox is checked.");
}
else if($(this).prop("checked") == false){
alert("Checkbox is unchecked.");
}
});
});
</script>
Using the jQuery :checked
Selector
You can also use the jQuery :checked
selector to check the status of checkboxes. The :checked
selector specifically designed for radio button and checkboxes.
<script type="text/javascript">
$(document).ready(function(){
$('input[type="checkbox"]').click(function(){
if($(this).is(":checked")){
alert("Checkbox is checked.");
}
else if($(this).is(":not(:checked)")){
alert("Checkbox is unchecked.");
}
});
});
</script>
- answered 7 years ago
- Community wiki
Your Answer