1

How can I apply toggleClass() to an input="checkbox" when it has been checked? I'm trying to add the class "calcButtonOn" when checked:

jQuery(function() {
    var total = 0;
    jQuery("input").click(function() {
        total = 0;
        jQuery("input:checked").not('input[value="reset"]').each(function() {
            total += parseFloat(jQuery(this).val());
        });
        jQuery("#Totalcost").html("Save up to " + total + "%");
    });

});
2
  • Listen to change event on your checkbox, then check its state with .prop('checked') Commented Mar 6, 2015 at 15:28
  • Do you also want to remove the class when the checkbox is unchecked, or should the class remain once it's added? Commented Mar 6, 2015 at 15:40

3 Answers 3

2

Select all the checkboxes with JQuery and add an event handler on change event:

jQuery("input[type=checkbox]").change(function(){
    jQuery(this).toggleClass("calcButtonOn", this.checked);
});

Example JSFiddle

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

1 Comment

This can get out of sync very easily. If the page is loaded with it checked then things would be reversed. You definitely need to check to see if it's checked in the change function before toggling the class.
1

I'd suggest, on the assumption that you want to have the class present when the element is checked, and removed when the element is subsequently unchecked:

jQuery('input[type=checkbox]').on('change', function () {
    $(this).toggleClass('active', this.checked);
});

jQuery('input[type=checkbox]').on('change', function () {
  $(this).toggleClass('active', this.checked);
});
input {
  display: block;
  padding: 0.5em;
  margin: 0 0 1em 0;
  box-shadow: none;
}

input.active {
  box-shadow: 0 0 1em 0.25em #f00;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />

Should any of your check-boxes be checked on page-load (via the checked attribute, for example) then it's worth further chaining the change() method (without a function callback) in order to fire the event:

jQuery('input[type=checkbox]').on('change', function () {
    $(this).toggleClass('active', this.checked);
}).change();

jQuery('input[type=checkbox]').on('change', function () {
  $(this).toggleClass('active', this.checked);
}).change();
input {
  display: block;
  padding: 0.5em;
  margin: 0 0 1em 0;
  box-shadow: none;
}

input.active {
  box-shadow: 0 0 1em 0.25em #f00;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" checked />
<input type="checkbox" />
<input type="checkbox" checked />
<input type="checkbox" />

References:

2 Comments

Definitely the clearest example in my opinion. I think you should add .trigger('change') after the .on so if the page is loaded with things checked it won't seem broken.
@Bill: that's an idea, certainly, but given the ambiguous and vague nature of the question I'm not sure it's a necessity. I'll edit that in, though, for completion :)
1

Use :

  $(".Your_Checkbox_Class")each(function(){
     if ($(this).is(':checked') ){
        $(this).toggleClass("Your_New_Class");
    } 
  });

Explanation : Loop thru all Checkboxes (hope you have given them Class as i gave above ).And then check if its checked , if yes toggle the class.

Edit

Well your question is not 100% clear. But if you want event on click of only one checkbox and toggle only that checkbox , then use

  $(".Your_Checkbox_Class").click(function(){
     var id = $(this).attr("id");
        $("#"+id).toggleClass("Your_New_Class");
  })

Explanation : If checkbox gets checked , then toggle class. I assume you have proper classes on DOM load . Like if after Loading DOM , if a checkbox is already ticked (Came from server side), it will already have class for checked_element already.

3 Comments

This will only toggle once but not when the state of the checkbox changes. Although it is not completely clear from the question if he wants this
Well I was not sure from OP's Question , there are 100 checkboxes or 1. I am editing.
You shouldn't use $.each($('.selector'), function() { }); Doing $('.selector').each(function () { }); is definitely preferred since you're working with a jQuery collection.

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.