jQuery addClass example

Change class name on click in jQuery

by joshmoto

HTML

<div class="clock">
    <div class="hrs">
      <div class="s-1">
        <div class="n-5">
          <time class="t-01"></time>
          <time class="t-02"></time>
          <time class="t-03"></time>
          <time class="t-04"></time>
          <time class="t-05"></time>
          <time class="t-06"></time>
          <time class="t-07"></time>
          <time class="t-08"></time>
          <time class="t-09"></time>
          <time class="t-10"></time>
          <time class="t-11"></time>
          <time class="t-12"></time>
          <time class="t-13"></time>
          <time class="t-14"></time>
          <time class="t-15"></time>
          <time class="t-16"></time>
          <time class="t-17"></time>
          <time class="t-18"></time>
          <time class="t-19"></time>
          <time class="t-20"></time>
          <time class="t-21"></time>
          <time class="t-22"></time>
          <time class="t-23"></time>
          <time class="t-24"></time>
        </div>
      </div>
      <div class="s-2">
        <div class="n-6">
          <time class="t-01"></time>
          <time class="t-02"></time>
          <time class="t-03"></time>
          <time class="t-04"></time>
          <time class="t-05"></time>
          <time class="t-06"></time>
          <time class="t-07"></time>
          <time class="t-08"></time>
          <time class="t-09"></time>
          <time class="t-10"></time>
          <time class="t-11"></time>
          <time class="t-12"></time>
          <time class="t-13"></time>
          <time class="t-14"></time>
          <time class="t-15"></time>
          <time class="t-16"></time>
          <time class="t-17"></time>
          <time class="t-18"></time>
          <time class="t-19"></time>
          <time class="t-20"></time>
          <time class="t-21"></time>
          <time class="t-22"></time>
          <time class="t-23"></time>
          <time class="t-24"></time>
        </div>
      </div> 
   </div>
   
   <div...

SCSS

.clock {
  padding: 40px;
  margin: 40px auto;
  width: calc(40px*8);
  font-family: SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace;
  font-size: .8rem;
  color: #e83e8c;
  
  .h-1,
  .h-2,
  .m-1,
  .m-2,
  .s-1,
  .s-2 {
    margin-bottom: 0px;
    width: 50%;
    float: left;
    position: relative;
    &:before {
      content: "";
      padding-top: 150%;
      display: block;
    }
    >DIV {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
    }
  }
  TIME {
    /* clock */
    float: left;
    width: calc(40px);
    height: calc(40px);
    display: block;
    background: #eeeeee;
    border-radius: 50%;
    position: relative;
    
    SPAN {
      position: absolute;
    }
    
    /* the hands */
    &:before,
    &:after {
      border-radius: 1%;
      content: "";
      display: block;
      width: 2px;
      height: 50%;
      background: #424242;
      left: 50%;
      position: absolute;
      transform: translateX(-1px);
      transform-origin: center bottom;
      transition: all 1s;
    }
  }
}


// rotate
@mixin time($hr,$mi) {
 
  $hour : 360/12*$hr;
  $min : 360/60*$mi;

  &:before {
    transform: rotate(#{$hour}deg);
  }
  &:after {
    transform: rotate(#{$min}deg);
  }
  
}


.n-0 {
  .t-01 {
    @include time(6,15);
  }
  .t-02 {
    @include time(9,15);
  }
  .t-03 {
     @include time(9,15);
  }
  .t-04 {
    @include time(9,30);
  }
  .t-05 {
    @include time(12,30);
  }
  .t-06 {
    @include time(6,15);
  }
  .t-07 {
    @include time(9,30);
  }
  .t-08 {
    @include time(12,30);
  }
  .t-09 {
    @include time(12,30);
  }
  .t-10 {
    @include time(12,30);
  }
  .t-11 {
    @include time(12,30);
  }
  .t-12 {
    @include time(12,30);
  }
  .t-13 {
    @include time(12,30);
  }
  .t-14 {
    @include time(12,30);
  }
  .t-15 {
    @include time(12,30);
  }
  .t-16 {
    @include time(12,30);
  }
  .t-17 {
    @include time(12,30);
  }
  .t-18 {
   ...

JavaScript

// remove class regex
$.fn.removeClassRegEx = function(regex) {
    var classes = $(this).attr('class');
    if (!classes || !regex) return false;
    var classArray = [];
    classes = classes.split(' ');
    for (var i = 0, len = classes.length; i < len; i++)
        if (!classes[i].match(regex)) classArray.push(classes[i]);
    $(this).attr('class', classArray.join(' '));
};
var val = 0;
getValue();
var interval = setInterval(function() {
	getValue();
}, 1000);

function getValue() {
	var date = new Date();
 	val = date.getSeconds();
	val = parseInt(val) + 1;
	val = getSec(val);
	setValue(val);
  
  console.log(val);
}

function setValue(val) {
		$('.s-1 > DIV, .s-2 > DIV').removeClassRegEx('n-');
		$('.s-1 > DIV').addClass('n-' + parseInt(val / 10));
		$('.s-2 > DIV').addClass('n-' + parseInt(val % 10));
}

function getSec(num) {
	if (num < 10 || num == 0) {
		num = '0' + num;
	} else if (num >= 60) {
		num = num % 60;
	}
	return num;
}

/* setInterval(function () {
	jQuery.each(seconds, function( sec, num ) {
		//console.log(sec);
		$('.s-1 > DIV, .s-2 > DIV').removeClassRegEx('n-');
		$('.s-1 > DIV').addClass('n-'+num[0]);
		$('.s-2 > DIV').addClass('n-'+num[1]);
	});
}, 1000); */