Created
November 27, 2018 15:36
-
-
Save DigitalKrony/524b5242f9aa24e234d5672dead9fc01 to your computer and use it in GitHub Desktop.
Adds a class with javascript via Object prototype.
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
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