2

I have a long list of checkboxes like so:

 <input class='masterCheckbox' type='checkbox' onclick='clickAll()' />

 <input class='modifyDb' type='checkbox' onclick='ajaxCall(someId)' />
 <input class='modifyDb' type='checkbox' onclick='ajaxCall(someId)' checked />
 <input class='modifyDb' type='checkbox' onclick='ajaxCall(someId)' checked />
 ... lets say there's exactly 100 ...
 <input class='modifyDb' type='checkbox' onclick='ajaxCall(someId)' />
 <input class='modifyDb' type='checkbox' onclick='ajaxCall(someId)' />
 <input class='modifyDb' type='checkbox' onclick='ajaxCall(someId)' checked />

The ajaxCall() function makes an ajax call (using jQuery) to a PHP page to make some change in the DB.

I want the masterCheckbox checkbox, when checked, to go through all the unchecked checkboxes and click them all, so that they each make a separate ajax call.

Using jQuery, I managed to get all the modifyDb checkboxes and then with the .each() function called a .click() on each element. This works visually; the checkboxes are toggled, but the ajax call is not made.

Is this because the ajax calls are made too fast? Can I queue up the function calls somehow?

3 Answers 3

4

Use trigger.
The following code reuns onclick of all .modifyDb

function clickAll() {
    $('.modifyDb').trigger('click');
}

Refference

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

7 Comments

ah, I completely forgot about trigger
Does this actually work? According to the docs .click() is just a shortcut for .trigger('click'), so either should work.
The OP is already using $.fn.click, which simply routes to $.fn.trigger, FYI: github.com/jquery/jquery/blob/1.9.1/src/event-alias.js#L9
so what you're saying is that I onclick='' will not trigger?
@EliteOctagon: Actually I just checked, and it seems to work fine at triggering onclick. So I'm not sure why .click() wasn't working for you.
|
2

I'd reorder this and get rid of all the inline event handlers. Something like this:

<input class='masterCheckbox' type='checkbox' />

<input class='modifyDb' type='checkbox' data-id='someId)' />
<!-- .... -->

And then bind them:

$(".modifyDb").click(function() {
    var id = $(this).data("id");
    ajaxCall(id);
});

And for the master check box:

$(".masterCheckbox").click(function() {
    $(".modifyDb").each(function() {
        var id = $(this).data("id");
        ajaxCall(id);
        // or if you really must, you could use trigger() here
    })
});

1 Comment

this helps out quite a bit
1

This would trigger the click event on boxes that are not already checked

function clickAllUnchecked(){
    $('.modyfDb').not(':checked').trigger('click')
}

1 Comment

I like the not() modifier, awesome attention to details

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.