Nathan L Nathan L - 4 months ago 15
Javascript Question

Applying if and else condition to add and remove class using jQuery

I have created the below code where I want to create an if/else condition where when the user clicks on the label, a class would be added and when unclicks, that class would be removed. I am stuck-up applying the same.

My code as below.

LIVE FIDDLE LINK

Complete working code here

HTML

<div class="customCheckboxBtns">
<div class="ck-button"><label><input checked="checked" name="column-2" type="checkbox">1</label></div>
<div class="ck-button"><label class="selected"><input checked="checked" name="column-3" type="checkbox">2</label></div><!-- This is how it will be added dynamically -->
<div class="ck-button"><label><input checked="checked" name="column-4" type="checkbox">3</label></div>
<div class="ck-button"><label><input checked="checked" name="column-5" type="checkbox">4</label></div>
<div class="ck-button"><label><input checked="checked" name="column-6" type="checkbox">5</label></div>
</div>


CSS

.customCheckboxBtns{
margin-bottom: 1rem;
margin-top: 0.25rem;
overflow:auto;
}

.ck-button {
margin-right: 0.375rem;
background-color:#ffffff;
border-radius:4px;
border:1px solid #26a69a;
overflow:auto;
float:left;
font-family: 'Source Sans Pro', sans-serif;
transition: all 0.2s ease 0s;
color:#999999;
}

.ck-button:hover {
background:#7dcac2;
border:1px solid #26a69a;
color:#26a69a;
background-color:#ffffff;
}

.ck-button label {
text-align:center;
padding: 0.25rem 0.5rem;
display:block;
background:#26a69a;
color:#ffffff;
transition: all 0.2s ease 0s;
}
.ck-button label:hover{
cursor:pointer;
}
.ck-button input:checked {
background: #26a69a none repeat scroll 0 0;
color: #ffffff;
padding: 0.25rem 0.5rem;
transition: all 0.2s ease 0s;
}
.ck-button label:hover{
background: #7dcac2 none repeat scroll 0 0;
color:#ffffff;
}
.ck-button label input {
position:absolute;
/*top:-2000px;*/
}
.ck-button label input[type="checkbox"] {
position: absolute;
visibility: hidden;
}
label.selected{
background: #7dcac2 none repeat scroll 0 0;
color:#ffffff;
}


SCRIPT

$('.ck-button label').on('click', function () {
$(this).addClass('selected');
});

$('.ck-button label').on('click', function () {
$(this).removeClass('selected');
});

Answer

Since you are trying to replicate checkbox behaviour, I would probably use something like that:

$('.ck-button input').on('change', function() {
  $(this.parentNode).toggleClass('selected', this.checked);
});
.customCheckboxBtns {
  margin-bottom: 1rem;
  margin-top: 0.25rem;
  overflow: auto;
}

.ck-button {
  margin-right: 0.375rem;
  background-color: #ffffff;
  border-radius: 4px;
  border: 1px solid #26a69a;
  overflow: auto;
  float: left;
  font-family: 'Source Sans Pro', sans-serif;
  transition: all 0.2s ease 0s;
  color: #999999;
}

.ck-button:hover {
  background: #7dcac2;
  border: 1px solid #26a69a;
  color: #26a69a;
  background-color: #ffffff;
}

.ck-button label {
  text-align: center;
  padding: 0.25rem 0.5rem;
  display: block;
  background: #26a69a;
  color: #ffffff;
  transition: all 0.2s ease 0s;
}

.ck-button label:hover {
  cursor: pointer;
}

.ck-button input:checked {
  background: #26a69a none repeat scroll 0 0;
  color: #ffffff;
  padding: 0.25rem 0.5rem;
  transition: all 0.2s ease 0s;
}

.ck-button label:hover {
  background: #7dcac2 none repeat scroll 0 0;
  color: #ffffff;
}

.ck-button label input {
  position: absolute;
  /*top:-2000px;*/
}

.ck-button label input[type="checkbox"] {
  position: absolute;
  visibility: hidden;
}

label.selected {
  background: #7dcac2 none repeat scroll 0 0;
  color: #ffffff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="customCheckboxBtns">
  <div class="ck-button">
    <label>
      <input name="column-2" type="checkbox">1</label>
  </div>
  <div class="ck-button">
    <label class="selected">
      <input checked="checked" name="column-3" type="checkbox">2</label>
  </div>
  <div class="ck-button">
    <label>
      <input name="column-4" type="checkbox">3</label>
  </div>
  <div class="ck-button">
    <label>
      <input name="column-5" type="checkbox">4</label>
  </div>
  <div class="ck-button">
    <label>
      <input name="column-6" type="checkbox">5</label>
  </div>
</div>

JSFiddle: https://jsfiddle.net/zdwjnLvL/15/