Created
May 18, 2014 00:56
-
-
Save danprince/05cb72052484355f3adc to your computer and use it in GitHub Desktop.
Quick jQuery style add/remove class implementation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
// Helper which turns class string into array | |
function getClassList(element) { | |
return element.getAttribute('class').split(' '); | |
} | |
// Add class with this name to the element | |
function addClass(name) { | |
var classes = getClassList(this); | |
if(classes.indexOf(name) < 0) { | |
classes.push(name); | |
} | |
this.setAttribute('class', classes.join(' ')); | |
} | |
// Remove class with this name | |
function removeClass(name) { | |
var classes, index; | |
classes = getClassList(this); | |
if((index = classes.indexOf(name)) >= 0) { | |
classes.splice(index, 1); | |
} | |
this.setAttribute('class', classes.join(' ')); | |
} | |
Element.prototype.addClass = addClass; | |
Element.prototype.removeClass = removeClass; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment