1

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

4 Answers 4

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

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

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

    });

});
Sign up to request clarification or add additional context in comments.

2 Comments

This looks pretty good, do you know what toString() will return in the case of no associated event?
This checks the events that have been bound, so in case of no bound event, this wont be running. By the way, this is good solution.
2

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');

Comments

0

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

Comments

0

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
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.