10 examples of 'javascript addclass' in JavaScript

Every line of 'javascript addclass' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
154function addClass(element_id, classname) {
155 document.getElementById(element_id).className += (" " + classname);
156}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

45function addClass(className) {
46 const firstChild = this.getFirstChild();
47
48 firstChild.addClass(className);
49}
18function addClass(className) {
19 this.classList.add(className);
20
21 return this;
22}
604function toggleClass(elt, cls) {
605 if (!elt) return;
606
607 var classArr = (elt.className || "").split(" ");
608 var classIndex = classArr.indexOf(cls);
609 if (~classIndex) {
610 classArr[classIndex] = null;
611 } else {
612 classArr.push(cls);
613 }
614
615 elt.className = classArr.join(" ").replace(/\s{2,}/g, ' ').replace(/^\s+|\s+$/g, '');
616}
81function addClass(element, className) {
82 if (element.classList) {
83 element.classList.add(className);
84 } else if (!hasClass(element, className)) {
85 element.className = element.className + ' ' + className;
86 }
87 return element;
88}
96toggleClass (cls) {
97 if (this.hasClass(cls)) {
98 this.removeClass(cls);
99 } else {
100 this.addClass(cls);
101 }
102}
25export function addClass(element: HTMLElement, className: string): void {
26 element.classList.add(className);
27}
19export function addClass(el, c) {
20 c != null && el[classList].add(c);
21}
352function addClass(el, c) {
353 c != null && el[classList].add(c);
354}
19function addJsClass(el) {
20 var classes = document.body.className.split(" ");
21 classes.push("js");
22 document.body.className = classes.join(" ");
23}

Related snippets