Last active
September 13, 2020 20:24
-
-
Save kieranbarker/8645fa081f7030857e4df812d0e2e044 to your computer and use it in GitHub Desktop.
Add a class to an element
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
/** | |
* Add a class to an element | |
* @param {Object|String} selector A reference to an element or a CSS selector string | |
* @param {String} className The class name | |
*/ | |
function addClass (selector, className) { | |
if (selector instanceof Element) { | |
selector.classList.add(className); | |
} else if (typeof selector === "string") { | |
document.querySelector(selector).classList.add(className); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment