Jordan Wallwork
Jordan Wallwork

Reputation: 3112

Using jQuery to select checkboxes with click events

Is there a way to use jquery to select all checkboxes on a page that have an associated click event? I considered adding a class, for instance HasClickEvent, that I could use to identify such classes, but I am editing a huge script where click events are sporadically added all over the place and I think this would probably end up being messier, so a single jQuery call would be perfect

Upvotes: 1

Views: 217

Answers (4)

James Allardice
James Allardice

Reputation: 166061

If the click events are attached using the onclick attribute (instead of added dynamically via JavaScript/jQuery), you can do it like this:

$("input[type=checkbox][onclick]").each(function() {
    //All returned elements have an onclick attribute
});

Upvotes: 0

SeanCannon
SeanCannon

Reputation: 78046

jQuery.each($('input[type=checkbox]').data('events'), function(i, event){

    jQuery.each(event, function(i, handler){

        if(handler.type.toString() == 'click')
        {
            // do something
        }

    });

});

Upvotes: 6

Sandeep Manne
Sandeep Manne

Reputation: 6092

To check all

$('.checkbox').attr('checked','checked'); // checkbox is the class for all checboxes to be selected change it with our own

To deselect all

$('.checkbox').removeAttr('checked');

Upvotes: 2

A quick google reveals this plugin, but it's pretty old. You may be able to read the code and see how they are achieving it though :D

Upvotes: 0

Related Questions