Skip to content

Instantly share code, notes, and snippets.

@kieranbarker
Last active Sep 13, 2020
Embed
What would you like to do?
Add a class to an element
/**
* 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