Skip to content

Instantly share code, notes, and snippets.

@kieranbarker
Last active September 13, 2020 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kieranbarker/8645fa081f7030857e4df812d0e2e044 to your computer and use it in GitHub Desktop.
Save kieranbarker/8645fa081f7030857e4df812d0e2e044 to your computer and use it in GitHub Desktop.
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