37

Is it possible to trigger change event on a checkbox using javascript/jquery?

Something like this (I run triggerChange on click of a button):

<label><input type="checkbox" id="chk"/>Label for chk</label>

<script>
function triggerChange(){
    $("#chk").trigger("change");
}
</script>

When I run the above code I get this error: "trigger is not a function".

5
  • 2
    Of course it is possible. Most probably you've forgotten to include jQuery. Commented Apr 17, 2013 at 9:09
  • I think you would be interested by this question: stackoverflow.com/questions/10159214/… Don't forget to get placed on a $(document).ready context ! Commented Apr 17, 2013 at 9:13
  • It sounds like $ returns something which is not a jQuery object. Which means $ might not be jQuery? Also note that triggering the change event will only run the change event handlers, it will not change the state of the checkbox. Commented Apr 17, 2013 at 9:16
  • Okay but how do you want it to be triggerred like change or click. Commented Apr 17, 2013 at 9:16
  • I'm not interested in click event. I have my reasons do not use the click. I want to trigger the change event without clicking the checkbox. Commented Apr 17, 2013 at 9:41

5 Answers 5

34

That trigger is not a function error message indicates something else is at play. According to this SO question:

What happens when a jQuery selector wasn't found?

no.good.at.coding says:

Do note however that you must ensure that selector is a jQuery object! Otherwise, you could get an error indicating that "trigger is not a function".

It's likely that you have forgotten jQuery?


As for your implementation, you should be fine the way you are using it. But trigger should be used to trigger event methods on elements that have already been attached via jQuery. Check out my demo:

Fiddle:
With click event: http://jsfiddle.net/fS4R5/1/
Without click event: http://jsfiddle.net/fS4R5/2/

HTML:

<label><input type="checkbox" id="chk"/>Label for chk</label>

JS:

function triggerChange(){
    $("#chk").trigger("change");
}

$("#chk").change(function() {
   alert("triggered!"); 
});

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

4 Comments

If not attached, the browser won't raise "trigger is not a function" exception. jQuery is tuned for it.
Thanks, updated my answer with details regarding the exception.
I'm not interested in click event. I have my reasons do not use the click. I want to trigger the change event without clicking the checkbox.
Can you add a fiddle working example that triggers that event without click?
10

Be sure that Input of Type checkbox is enabled, in case is disabled trigger will not fire event

//fire event 
$('#ceckBoxId').click();
$('#ceckBoxId').trigger('click');

or change checkbox checked val

$('#ceckBoxId').prop('checked', true);
$('#ceckBoxId').prop('checked', false);

1 Comment

my control was disabled, so it was not working. Thank you!!
6

In jQuery, you can usually trigger an event by calling it's eventhandler method withoud any function parameters.

For example a click handler can be assigned as such:

$('#mything').click(function(e){dostuff});

the click event in itself can be triggered by simply running: $('#mything').click();

I suspect this can be done for every existing event in jQuery.

4 Comments

You can also call it with .trigger("click"). There is nothing wrong with it.
I'm not interested in click event. I have my reasons do not use the click. I want to trigger the change event without clicking the checkbox.
Well, 'click' was only an example. You might as well replace it with the function 'change'. For reference: api.jquery.com/change
could you put an example. It will be better
2

I think the preferred method since 1.9.1 is 'on'. Specially if you use dynamically added checkboxes.

Say you have a div with id='divCOntent' and on it is a checkbox with id='cballaut', you could do this

$('#divcontent').on('click', '#cballaut', function (e) {
                    alert(this.checked);
                });

1 Comment

That hardly answer the question.
0

use prop method.

$('#myCheck').prop('checked', true);
$('#myCheck').prop('checked', false);

1 Comment

This won't trigger a change event.

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.