Seansa Seansa - 2 months ago 11
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:

http://jsfiddle.net/nrYec/194/

JS:

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

MJH MJH
Answer

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="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

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