Create a gist now

Instantly share code, notes, and snippets.

Add / Remove class without using jQuery
/*
* This code is to add or remove class depends on the direction like Smartphone.
* The classname is 'isActive'.
*/
function toogleClass(){
this.node = document.querySelector(target);
}
toogleClass.prototype.init = function(){
var self = this;
window.addEventListener('resize', function(){ self.detection() }, false);
window.addEventListener('load', function(){ self.detection() }, false);
};
toogleClass.prototype.detection = function(){
var self = this;
if( window.innerHeight > window.innerWidth ){
self.removeClass();
} else {
self.addClass();
}
};
toogleClass.prototype.addClass = function(){
var self = this;
if (self.node.classList) {
self.node.classList.add('isActive');
} else {
self.node.className += ' ' + 'isActive';
}
};
toogleClass.prototype.removeClass = function(){
var self = this;
if (self.node.classList) {
self.node.classList.remove('isActive');
} else {
self.node.className = self.node.className.replace(new RegExp('(^|\\b)' + 'isActive'.split(' ').join('|') + '(\\b|$)', 'gi'), ' ');
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment