10 examples of 'add multiple class jquery' in JavaScript

Every line of 'add multiple class jquery' 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
19function add(el, className) {
20 if (el.classList) {
21 el.classList.add(className);
22 return;
23 }
24 var current = el.className;
25 if (!current.length) {
26 el.className = className;
27 } else if (!lookupClass(className).test(current)) {
28 el.className += ' ' + className;
29 }
30}
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

91function removeAllElementClass(className){
92 $("."+className, window.parent.document).removeClass(className);
93}
65export function toggleClass(elem, className, add) {
66 if (add === true) return addClass(elem, className);
67 if (add === false) return removeClass(elem, className);
68 return hasClass(elem, className) ? removeClass(elem, className) : addClass(elem, className);
69}
104addClassName(name) { this.classNames.push(name.toLowerCase()); }
75export function addClass(element, classname) {
76 const classList = element.classList;
77 if (classList) {
78 classList.add(classname);
79 }
80 else {
81 const cls = className(element).split(' '),
82 map = {};
83 each(cls, (klass) => {
84 map[klass] = true;
85 });
86
87 if (!map[classname]) {
88 className(element, cls.join(' ') + classname);
89 }
90 }
91}
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}
221export function addClass(elem, className) {
222
223 // Check if there is an element and
224 // a Class Name given
225 if( !elem || !className ) return null;
226
227 // Check if it is a list of elements
228 if( elem.length > 1 ) {
229
230 // Run through the list and add
231 // Class Name to all of them
232 for( var i=0, max=elem.length; i<max; i++ ) {
233 addClass( elem[i], className );
234 }
235
236 // Extend all Elements and return it
237 return elem;
238
239 } else {
240
241 // Check if browser supports classList
242 // Jep: Add it to classList
243 // Nope: Add it as string
244 if( !!elem.classList ) {
245 elem.classList.add( className );
246 } else if( !hasClass( elem, className ) ) {
247 elem.className += " " + className;
248 }
249
250 // return the extended Element
251 return elem;
252 }
253}
18function addClass(className) {
19 this.classList.add(className);
20
21 return this;
22}
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}
2296function addClassName(element, className) {
2297 if (!(element = $(element))) return;
2298
2299 if (!hasClassName(element, className))
2300 element.className += (element.className ? ' ' : '') + className;
2301
2302 return element;
2303}

Related snippets