Jquery detect when a checkbox is checked

Asked
Active3 hr before
Viewed126 times

7 Answers

checkboxjquerychecked
90%

Example_snippet/controller/utility/_checkbox.js/ //using plane javascript if(d. . .
//using plane javascript 
if (document.getElementById('on_or_off_checkbox').checked) {
   //I am checked
}

//using jQuery
if ($('#on_or_off_checkbox').is(':checked')) {
   //I am checked
}
load more v
88%

How can I deal with the cancellation and keep textbox value consistent with the check state?,This leaves things in an inconsistent state and the textbox says false when the checkbox is checked,,Here

Example_snippet/controller/utility/_checkbox.js/ $(document).ready(function() {. . .
$(document).ready(function() {
   //set initial state.
   $('#textbox1').val(this.checked);

   $('#checkbox1').change(function() {
      if (this.checked) {
         var returnVal = confirm("Are you sure?");
         $(this).prop("checked", returnVal);
      }
      $('#textbox1').val(this.checked);
   });
});
Step 2 continued with $(document).ready(function() {. . .
$(document).ready(function() {
   //set initial state.
   $('#textbox1').val($(this).is(':checked'));

   $('#checkbox1').change(function() {
      if ($(this).is(":checked")) {
         var returnVal = confirm("Are you sure?");
         $(this).attr("checked", returnVal);
      }
      $('#textbox1').val($(this).is(':checked'));
   });
});
load more v
72%

  Instead of using jQuery’s is() or prop() method, you can directly access the ,checked property of the DOM element to determine whether the checkbox is checked

Example_snippet/controller/utility/_checkbox.js/ :checked. . .
: checked
load more v
65%

The jQuery prop() method provides a simple, effective, and reliable way to track down the current status of a checkbox, It works pretty well in all conditions because every checkbox has a checked property which specifies its checked or unchecked status

Example_snippet/controller/utility/_checkbox.js/ <script> $(document).ready. . .
<script>
    $(document).ready(function(){
        $('input[type="checkbox"]').click(function(){
            if($(this).prop("checked") == true){
                console.log("Checkbox is checked.");
            }
            else if($(this).prop("checked") == false){
                console.log("Checkbox is unchecked.");
            }
        });
    });
</script>
load more v
75%

It’ll be checked by default because of the checked attribute, jQuery allows you to check a DOM element’s attribute through the

Example_snippet/controller/utility/_jquery.js/ <input type="checkbox" name="a. . .
<input type="checkbox" name="agree" checked>I agree to stuff
load more v
40%

The jQuery prop() method is basically used to get and set properties of a form control, But here we will check if the checked property of the checkbox is there or not

Example_snippet/controller/utility/_jquery.js/ <script> $(functio. . .
    <script>
        $(function () {
            $("input:checkbox").on("change",function(){
		if($(this).prop("checked")){
                    console.log("Checkbox Checked!");
                }else{
                    console.log("Checkbox UnChecked!");
                }
            });
        });
    </script>
load more v
22%

How to check whether a checkbox is checked in jQuery?,prop() and is() method are the two way by which we can check whether a checkbox is checked in jQuery or not,,How to check if an element is hidden in jQuery?,How to know which radio button is selected using jQuery?

Example_snippet/controller/utility/_jquery.js/ $(selector).prop(parameter) . . .
$(selector).prop(parameter)
load more v