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.

2

4 Answers 4

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>

Sign up to request clarification or add additional context in comments.

4 Comments

I'm guessing that in practice, the actual checkbox is hidden.
@JamieBarker it doesn't matter, since they are contained in a label. Wherever you click in the label it will toggle the checkbox.
Thanks but on one of your codes, there are times that the elements are not contained in a label so I use the .click() event not .change(). +1
@ChainTrap that is cool. I was going with the original html in your question. Whatever works better :)
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));

5 Comments

This could be simplified as $(this).hasClass('checked-state') is a Boolean value, toggleClass could be used...I was suggesting .toggleClass( className, state ) approach...
I'll try this one. Will be back soon :)
Thanks for this :)
@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?
@Satpal It was working on a :checkbox which is not contained in a <label>. I've made a fiddle using Bootstrap 3 framework. Please see it and tell me what's wrong with it. Thank you.
0

Try this

$('#yourCheckboxSelector').prop('checked', true);

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.