4

Hi I was wondering if there is an event for checkboxes that can be triggered by other code rather than by a user...

e.g.

http://jsfiddle.net/DC3EH/

For the "Choose All" checkbox, it should automatically also toggle the "selected" class to toggle the row's background colour.

I've tried "change" and "click" and that only is triggered by the user.

I commented out some code... BTW I want to have a lot of checkboxes that toggle certain classes of checkboxes so I want to avoid having lots of ".closest('tr').addClass('selected');" etc.

4 Answers 4

4

It seems to work perfectly:

http://jsfiddle.net/DC3EH/20/

I used .change()

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

Comments

0

I'm not sure this is what you need, but what I figured is that all <tr> must changed to selected by check the "Choose All" checkbox. try this http://jsfiddle.net/DC3EH/5/

1 Comment

In the full version certain rows are checked or unchecked depending on their attributes... so a closest('tr') would be used...
0

Have you tried triggering the click event?

.trigger("click") // for each checkbox

Example: http://jsfiddle.net/ELTRd/1/

1 Comment

The group is the inverted version of the "Choose All" checkbox... also the yellow row colouring is inverted...
0

If you bind to the change event, you can trigger the click event in jquery which will check the checkbox and in turn fire the change event:

$(document).ready(function(){
    $('#check_all').click(function(event) {
        $('[name^="checkbox"]').trigger("click");
    });   

    $('[name^="checkbox"]').change(function(event) {
        console.log('a');
        if ($(this).is(":checked")) {
             $(this).closest('tr').addClass('selected');
        }
        else {
             $(this).closest('tr').removeClass('selected');
        }
    });    
});​

Updated Fiddle: http://jsfiddle.net/DC3EH/19/

The reason I switched to the change event instead of the click event is how jquery processes the triggering of events. When you trigger click it will call your event handler before it changes the state of the checkbox. Binding to the change ensures your event doesn't get called until after the state has changed.

2 Comments

Works fine in chrome and FF for me.
It doesn't seem to work in IE8. Also in Chrome and FF the "Choose All" checkbox just inverts the current selection... it should turn all the other checkboxes on or off depending on the "Choose All" checkbox state.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.