4 examples of 'javascript set class' in JavaScript

Every line of 'javascript set class' 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
68function addSetProp(clazz, className) {
69 clazz.prototype.getProp = function(key, create) {
70 return getProp(this.propsObj, key, create);
71 };
72 clazz.prototype.set = function(key, val, allowExtant) {
73 if (typeof key == 'string') {
74 if (val === undefined) return this;
75 var prop = this.getProp(key, true);
76 if (prop.exists) {
77 if (!deq(prop.val, val)) {
78 if (!allowExtant) {
79 l.error(
80 "%s(%s) overwriting %s: %s -> %s",
81 className,
82 JSON.stringify(this.findObj),
83 key,
84 JSON.stringify(prop.val),
85 JSON.stringify(val)
86 );
87 }
88 prop.set(val);
89 this.toSyncObj[key] = val;
90 this.dirty = true;
91 }
92 } else {
93 prop.set(val);
94 this.toSyncObj[key] = val;
95 this.dirty = true;
96 }
97 } else if (typeof key == 'object') {
98 for (k in key) {
99 this.set(k, key[k], !!val);
100 }
101 } else {
102 l.error(
103 "%s(%s): Invalid .set() argument: %s",
104 className,
105 JSON.stringify(this.findObj),
106 JSON.stringify(key)
107 );
108 }
109 return this;
110 }
111}
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

104function setClassName(id, className) {
105 var ele = document.getElementById(id);
106 if (ele) {
107 ele.className = className;
108 }
109}
3function toggleClass(element: Element, klass: string, value: boolean) {
4 const classes = new Set(element.className.split(" "));
5 if (value) {
6 classes.add(klass);
7 } else {
8 classes.delete(klass);
9 }
10 element.className = Array.from(classes).join(" ");
11}
96toggleClass (cls) {
97 if (this.hasClass(cls)) {
98 this.removeClass(cls);
99 } else {
100 this.addClass(cls);
101 }
102}

Related snippets