I'm trying to check a checkbox using jQuery and trigger the onclick event in the process.
Say I have a checkbox defined in html:
<input type=checkbox value=checked name=check1 onclick=alert(this.value);>
And I have a jQuery statement that is triggered in a function:
$('input[name=check1]').attr('checked', true);
The result is the checkbox gets checked but the javascript onclick event is not triggered (hence no alert). But if I were to trigger the click even manually as such:
$('input[name=check1]').attr('checked', true).trigger('click');
The result is the checkbox gets checked, javascript onclick event is triggered (and the value is correctly gotten) but then the checkbox gets unchecked after that.
Can anyone tell me how I can achieve what I'm trying to do?