Trigger onclick function of input that has been checked automatically with jQuery

718 Views Asked by At

I have a form with some checkboxes which go like this:

<input type="checkbox" id="id6" value="3" onclick="optionselect('day','3','id6','50')" />

Conditioned on another input I can have these checkboxes checked automatically but for some reason I can't trigger the "optionselect".

I've tried some of the following, but nothing works:

$("#id6").prop("checked", true).trigger("click");
$("#id6").prop("checked", true).triggerHandler("click");
$("#id6").prop("checked", true).click();

FYI, "optionselect" just does a bit of math in some other text inputs.

1

There are 1 best solutions below

4
On

I think that problem is that you should use onchange event in this case, not click. Try this:

<input type="checkbox" id="id6" value="3" onchange="optionselect('day','3','id6','50')" />

and

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

Basically .click() effectively unchecks checkbox after you checked it with prop('checked', true).