10 examples of 'removeclass in javascript' in JavaScript

Every line of 'removeclass in javascript' 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
96toggleClass (cls) {
97 if (this.hasClass(cls)) {
98 this.removeClass(cls);
99 } else {
100 this.addClass(cls);
101 }
102}
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

274function removeClass(className) {
275 if(!className) {
276 // remove all classes from all matched elements
277 this.each(function(el) {
278 el.removeAttribute(attr);
279 });
280 return this;
281 }
282 var classes = className.split(/\s+/);
283 this.each(function(el) {
284 var val = el.getAttribute(attr);
285 // no class attribute - nothing to remove
286 if(!val) { return }
287 var names = val.split(/\s+/);
288 names = names.filter(function(nm) {
289 return ~classes.indexOf(nm) ? false : nm;
290 })
291 el.setAttribute(attr, names.join(' '));
292 })
293 return this;
294}
48function removeClass(className) {
49 if(!className) {
50 // remove all classes from all matched elements
51 this.each(function(el) {
52 el.removeAttribute(attr);
53 });
54 return this;
55 }
56 var classes = className.split(/\s+/);
57 this.each(function(el) {
58 var val = el.getAttribute(attr);
59 // no class attribute - nothing to remove
60 if(!val) {
61 return;
62 }
63 var names = val.split(/\s+/);
64 names = names.filter(function(nm) {
65 return ~classes.indexOf(nm) ? false : nm;
66 });
67 el.setAttribute(attr, names.join(' '));
68 });
69 return this;
70}
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}
42function removeClass(className) {
43 this.classList.remove(className);
44
45 return this;
46}
22function removeClass(el, className) {
23 el.classList.remove(className);
24}
5function removeClass (el, className) {
6 if (el.classList) { el.classList.remove(className) } else { el.className = el.className.replace(new RegExp('(^|\\b)' + className.split(' ').join('|') + '(\\b|$)', 'gi'), ' ') }
7}
44export function removeClass(element, classname) {
45 if(element && classname) {
46 while(element.className.indexOf(classname) >= 0) {
47 element.className = element.className.replace(classname, '');
48 }
49 }
50};
166function removeClass(el, cls) {
167 var old = el.className;
168 var newCls = ' ' + el.className + ' ';
169 newCls = newCls.replace(new RegExp(' (' + cls + ' +)+','g'), ' ');
170 el.className = newCls.replace(/(^ +)|( +$)/g, '');
171}
399function removeClass(el, className) { el.removeClass(className); }

Related snippets