0

I have a class that adds a fake checkbox and using jQuery, once the user clicks it, add the checked state class to the fake checkbox.

CSS

.fake-checkbox {  /* ... */  }
.fake-checkbox.checked-state {  /* ... */  }

HTML

<label>
  <input type="checkbox">
  <div class="fake-checkbox"></div>
</label>

JS

(function($) {
  $('.fake-checkbox').click(function() {
    // Check if fake-checkbox has class checked-state, then remove the class checked-state and vice versa.
    if ($(this).hasClass('checked-state')) {
      $(this).removeClass('checked-state');
    } else {
      $(this).addClass('checked-state');
    }
  });
}(jQuery));

Now, I also want to make the input checkbox in its checked state at the same time when the class is added and in its unchecked state when the class is removed.

I know this can be done with element.checked = true but not in jQuery.

How can I achive this?

EDIT

This is surely different and not a duplicate of this question cause we're in a different case, although there's a similarity about 'ticking a checkbox using jQuery' but still not a possible duplicate.

Thanks.

Community
  • 1
  • 1
Chain Trap
  • 130
  • 1
  • 12
  • 3
    Possible duplicate of [Setting "checked" for a checkbox with jQuery?](http://stackoverflow.com/questions/426258/setting-checked-for-a-checkbox-with-jquery) – jonhopkins Sep 26 '16 at 12:31
  • 2
    I would urge you to look at another one of your answers: http://stackoverflow.com/a/39703074/2117156 – Jamie Barker Sep 26 '16 at 12:49

4 Answers4

2

Besides the jQuery answers, i would like to suggest (for this specific case) a CSS only solution, since the checkbox and the .fake-checkbox are siblings.

CSS

.fake-checkbox {  /* ... */  }
:checked + .fake-checkbox{  /* ... */  }

HTML

<label>
  <input type="checkbox">
  <div class="fake-checkbox"></div>
</label>

Demo

.fake-checkbox {  color:#ccc;  }
:checked + .fake-checkbox{  color:green;  }
<label>
  <input type="checkbox">
  <div class="fake-checkbox">fake</div>
</label>

As for a jQuery answer i would suggest you monitor the state of the actual checkbox instead of manually testing the states.

$('label :checkbox').on('change', function(){
  $(this)
    .siblings('.fake-checkbox')
    .toggleClass('checked-state', this.checked);
})

Demo

$('label :checkbox').on('change', function(){
  $(this)
    .siblings('.fake-checkbox')
    .toggleClass('checked-state', this.checked);
})
.fake-checkbox {  color:#ccc;  }
.fake-checkbox.checked-state {  color:green;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<label>
  <input type="checkbox">
  <div class="fake-checkbox">fake</div>
</label>
Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
0

Try this

$('#yourCheckboxSelector').prop('checked', true);
Aleksandar Matic
  • 789
  • 1
  • 9
  • 21
0

You can check a checkbox using JQuery. Using the prop method. https://learn.jquery.com/using-jquery-core/faq/how-do-i-check-uncheck-a-checkbox-input-or-radio-button/

Use this

HTML

<label>
  <input id="real-checkbox" type="checkbox">
  <div class="fake-checkbox"></div>
</label>

JS

(function($) {
  $('.fake-checkbox').click(function() {
    // Check if fake-checkbox has class checked-state, then remove the class checked-state and vice versa.  
    if ($(this).hasClass('checked-state')) {
        $(this).removeClass('checked-state');      
        $( "#real-checkbox" ).prop( "checked", false );
    } else {
        $(this).addClass('checked-state');
        $( "#real-checkbox" ).prop( "checked", true );
    }
  });
}(jQuery));
Isabel Inc
  • 1,871
  • 2
  • 21
  • 28
0

As the checkbox is immediate preceding sibling, you can use .prev() then set the property using .prop() method

(function($) {
  $('.fake-checkbox').click(function() {
    // Check if fake-checkbox has class checked-state, then remove the class checked-state and vice versa.  
    if ($(this).hasClass('checked-state')) {
        $(this).removeClass('checked-state');      
        $(this).prev(':checkbox').prop('checked', false);
    } else {
        $(this).addClass('checked-state');
        $(this).prev(':checkbox').prop('checked', true);
    }
  });
}(jQuery));

Above code can be simplified as

(function($) {
  $('.fake-checkbox').click(function() {
    $(this).toggleClass('checked-state');
    $(this).prev(':checkbox').prop('checked', $(this).hasClass('checked-state'));
  });
}(jQuery));
Satpal
  • 132,252
  • 13
  • 159
  • 168
  • 1
    This could be simplified as `$(this).hasClass('checked-state')` is a `Boolean` value, `toggleClass` could be used...I was suggesting `.toggleClass( className, state )` approach... – Rayon Sep 26 '16 at 12:34
  • I'll try this one. Will be back soon :) – Chain Trap Sep 26 '16 at 12:38
  • Thanks for this :) – Chain Trap Sep 26 '16 at 12:47
  • @Satpal Also, there's a conflict for `:checkbox` that already has the `checked` attribute. On first click, the class is removed but `:checkbox` is still checked, then on another clicks it became opposite (when the class is added, `:checkbox` is not checked` and vice versa). How can I fix this? – Chain Trap Sep 26 '16 at 14:59
  • @Satpal It was working on a `:checkbox` which is not contained in a ` – Chain Trap Sep 27 '16 at 03:45