add a method to class js code example
Example 1: javascript add class
var myElement = document.querySelector('#myElement');
myElement.classList.add('some-class');
myElement.classList.add('one-class', 'one-more-class');
myElement.classList.remove('any-class');
Example 2: js add class
const div = document.createElement('div');
div.className = 'foo';
console.log(div.outerHTML);
div.classList.remove("foo");
div.classList.add("anotherclass");
console.log(div.outerHTML);
div.classList.toggle("visible");
div.classList.toggle("visible", i < 10 );
console.log(div.classList.contains("foo"));
div.classList.add("foo", "bar", "baz");
div.classList.remove("foo", "bar", "baz");
const cls = ["foo", "bar"];
div.classList.add(...cls);
div.classList.remove(...cls);
div.classList.replace("foo", "bar");
Example 3: es6 class example
class Polygon {
constructor(height, width) {
this.height = height;
this.width = width;
}
}
Example 4: es6 class example
var Polygon = class {
constructor(height, width) {
this.height = height;
this.width = width;
}
}