10 examples of 'classlist.add' in JavaScript

Every line of 'classlist.add' 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
23public add(...classNames: string[]): void {
24 const attr = this.ownerElement.getAttribute('class');
25 const list = attr ? attr.split(' ') : [];
26 for (const className of classNames) {
27 if (!list.includes(className)) {
28 list.push(className);
29 }
30 }
31 this.ownerElement.setAttribute('class', list.join(' '));
32}
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

18function addClass(className) {
19 this.classList.add(className);
20
21 return this;
22}
348export function add__class(el, class_name) {
349 return el.classList.add(class_name)
350}
25export function addClass(element: HTMLElement, className: string): void {
26 element.classList.add(className);
27}
38export function addClass (dom, cls) {
39 if (typeof dom === 'string') {
40 dom = qs(dom)
41 }
42 dom.classList.add(cls)
43 return dom
44}
352function addClass(el, c) {
353 c != null && el[classList].add(c);
354}
19export function addClass(el, c) {
20 c != null && el[classList].add(c);
21}
72add: function addClass(el, className) {
73 return el.classList.add(className);
74},
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}
3function addOrRemoveClass(el, className, add) {
4 el.classList[add ? 'add' : 'remove'](className);
5}

Related snippets