Programmatically changing checkbox does not fire change event

javascript, jquery, jquery-events

Solution

You need to call change() or use trigger() to trigger the `change` event when values are changed through code.

Using `.change()`

$('#gridlines').prop('checked', true).change();

Using `.trigger()`

$('#gridlines').prop('checked', true).trigger("change");

Problem

I have the following code here: ``` $('input[type="checkbox"][id="gridlines"]').change(function () { alert('hello world'); }); $('#gridlines').prop('checked', true); ``` When I load my page, the checkbox is checked, but the "hello world" does not get prompted. However, when I click on the checkbox manually, "hello world" gets prompted. What gives?

Original source