mardi 28 juillet 2015

bootstrap checkbox changes class when checked

I'm using bootstrap's checkbox and currently looking for a way with jQuery that will allow my checkbox to change class when checked.

<form>
  <div class="checkbox">
    <input type="checkbox" name="checkbox">
    <label for="checkbox">I am not a Robot!</label>
  </div> <!-- //.checkbox -->
</form>

I've tried the following...it's not working as I wanted. Have I made a mistake somewhere?

(document).ready(function() {
  if ($('.checkbox').is(':checked')) {
    $(".checkbox").removeClass('has-error').addClass('has-success'); // checked
  } else {
    $(".checkbox").addClass('has-error'); //unchecked
  }
});

JsFiddle: http://ift.tt/1HZq3wR

If there's a better way to do this, please comment! Any help will be appreciated. :)




Aucun commentaire:

Enregistrer un commentaire