Skip to content

Latest commit

 

History

History
27 lines (19 loc) · 644 Bytes

add_remove_class.md

File metadata and controls

27 lines (19 loc) · 644 Bytes

Add or remove a class

We're going to look at all the elements with a class of cat, and then test for a class, remove a class, and add a class.

Everything you need to do with a class. Pretty much.

for(const cat of $(".cat")) {
	if (cat.classList.contains('fleas')) {
		// The cat has the fleas class... remove it
		this.classList.remove('fleas');
		this.classList.add('clean');
	}
}

(I just noticed that this example uses a little helper function I keep handy...

function $(selector) {
	return document.querySelectorAll(selector);
}

...and similarly I use:

function $id(id) {
	return document.getElementById(id);
}

...)