Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

detect checkbox state change

Tags:

jquery

I want to detect if user checks or unchecks a check box within an iframe and set a flag like so. But it does not seem to work as expected. Any pointers on the error on the code?

jQuery("#frameName").contents().find(":checkbox").bind('change', function(){        
            val = true;
            alert("changed");
    });

But I can detect if a checkbox is checked by using

var val = jQuery("#frameName").contents().find(":checkbox").is(":checked");

I want to be able to detect any state change on a checkbox and set the flag.

like image 965
neelmeg Avatar asked Oct 27 '11 17:10

neelmeg


1 Answers

Use the checked DOM property to check whether a checkbox is checked or not. The change event is always triggered on change, even if the checkbox gets unchecked.

jQuery("#frameName").contents().find(":checkbox").bind('change', function(){        
        val = this.checked; //<---
        alert("changed");
});
like image 157
Rob W Avatar answered Sep 25 '22 04:09

Rob W