0

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 Answers1

0

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).

dfsq
  • 191,768
  • 25
  • 236
  • 258
  • Still doesn't work. The checkbox is checked but it doesn't trigger the function (it should add 3 to a certain number, which in this case is 0 and would turn to 3 if "optionselect" triggers). –  Feb 14 '14 at 07:24
  • Check console for errors. `onchange` handler is definitely called. It might be a problem with `optionselect` then. Show its code. – dfsq Feb 14 '14 at 07:31
  • The only thing I get in the console is `event.returnValue is deprecated. Please use the standard event.preventDefault() instead.`. `optionselect` works fine if I click it normally. –  Feb 14 '14 at 07:58
  • Ok, so I guess we can only help if you provide a fiddle with the problem. Otherwise there is no way to find out. – dfsq Feb 14 '14 at 09:07