Skip to content

Instantly share code, notes, and snippets.

@DigitalKrony
Created November 27, 2018 15:36
Show Gist options
  • Save DigitalKrony/524b5242f9aa24e234d5672dead9fc01 to your computer and use it in GitHub Desktop.
Save DigitalKrony/524b5242f9aa24e234d5672dead9fc01 to your computer and use it in GitHub Desktop.
Adds a class with javascript via Object prototype.
Object.prototype.addClass = function (string) {
var classList = this.className.split(' ');
var classesToAdd = string.split(' ');
for (var toAdd of classesToAdd) {
var index = classList.indexOf(toAdd);
if(index === -1) {
classList.push(toAdd);
}
}
this.classList = classList.join(" ");
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment