class change eventListener Javascript code example

Example: when classes change event listener js

const mutationObserver = new MutationObserver(mutationsList => {
      mutationsList.forEach(mutation => {
		if (mutation.attributeName === 'class') {
			// classes have changed
		}
	});
});

mutationObserver.observe(
	// a document object you want to watch. some examples of document objects:
	// document.getElementById('id')
	// document.getElementsByClassName('class')[5]
	// document.querySelector('[query=selector]')
	// etc
	,{ attributes: true }
)

// when you wanr to remove the listener
mutationObserver.disconnect();