three3
three3

Reputation: 2846

Trigger onclick event with Javascript

I am trying to trigger an onclick event via Chrome Developer Tools that will check all checkboxes on the page and also trigger the onclick event of all checkboxes on the page.

Here is a sample checkbox on the page:

<input type="checkbox" onclick="ctl00_ContentAreaHolder1_uctlLocationSelectionControl_AccountStructureTreeView.HandleCheck(this,'p_686660',4);" name="checker_p_686660">

Here is my code to check all checkboxes on the page:

(function() {
    var aa= document.getElementsByTagName("input");
    for (var i =0; i < aa.length; i++){
        if (aa[i].type == 'checkbox')
            aa[i].checked = true;
    }
})()

The only thing I cannot figure out is how to trigger the onclick event when the checkbox is checked. Any help is appreciated!

Upvotes: 0

Views: 4198

Answers (3)

Madnx
Madnx

Reputation: 1564

You may want to do that:

$('input[type="checkbox"]').each(function(){
    $(this).trigger('click');
    // Will check all the checkbox and trigger the event
});

or simply:

$('input[type="checkbox"]').each(function(){
    $(this).click();
    // Will check all the checkbox and trigger the event
});

or you can do it, using your code:

(function() {
    var aa= document.getElementsByTagName("input");
    for (var i =0; i < aa.length; i++){
        if (aa[i].type == 'checkbox')
            aa[i].click()
    }
})()    

Upvotes: 1

Rakesh Kumar
Rakesh Kumar

Reputation: 2853

Try this solution

<input type="checkbox" name="checker_p_686660">

Jquery Code

$('input').on('change', function(){

    if( $(this).prop('checked', true) ){
        alert('checked');
    }

})

Working Example: http://jsfiddle.net/2p64P/1/

Upvotes: 0

Milind Anantwar
Milind Anantwar

Reputation: 82231

For checking all the checkboxes:

 $("input[type=checkbox]").each(function () {
            $(this).prop("checked", true);
        });

For triggering to check all:

 $("input[type=checkbox]").each(function () {
            $(this).trigger('click');
        });

Upvotes: 0

Related Questions