Function on ANY checkbox change on the page

I'm new to JQuery in general and I want to have a function activate when ANY checkbox on the page is checked or not. Then it should check what state the checkbox is "checked" or not.

I'm not sure how to do this as all the examples I'd seen require the name of the checkbox in order to work. I'd be fine if this was in Javascript aswell.

Anyone have any ideas?

Upvotes: 0

Views: 126

Answers (2)

sajawikio
sajawikio

Reputation: 1514

Bind dynamically your checkboxes to change event, and check if they are checked, then do a function of your choice.

$(document).on("change", ".chkelement", function(){
   if( $(this).is(":checked") )
   {
      //DO SOMETHING
   }
});

I recommend use a container other than "document", as close as possible to where checkboxes will be, but you get the idea right?

Upvotes: 1

j08691
j08691

Reputation: 207861

$('input[type="checkbox"]') would select all checkboxes on your page.

The following would run a function when any of your checkboxes are changed, and when checked:

$('input[type="checkbox"]').change(function(){
    if($(this).is(':checked')) {//do something}
})

Upvotes: 4

Related Questions