Seansa Seansa - 1 year ago 105
Javascript Question

Jquery Checkbox remains check on click

I have a checkbox that needs to remain check once user click on the input box.

Issue: On First Click the checkbox is checked, which works fine. When input is clicked again, the checkbox becomes unchecked.

Output Needed:

I want the checkbox to stay checked as this triggers a change event.

I have searched many examples to find this solution but could not find it.

Any help is appreciated.

Please See Fiddle:


$('input').click(function() {
$('#x').text(this.checked ? "is checked" : "is not checked");
$('.b2').click(function() {

Answer Source

Try this:

$('.b2').click(function() {
    if (!$('.trigger').prop('checked')) {
        $('.trigger').prop('checked', 'true');

$('input').click(function() {
    $('#x').text($('.trigger').prop('checked') ? "is checked" : "is not checked");
<script src=""></script>

<input class='trigger' type='checkbox'>
<input class='b2' type='text' name='something'>
<input class='b2' type='text' name='something'>
<div id='x'></div>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download