Javascript Catch change class event

How to catch event when element change a class html <div id=”foo”>Click a Button!</div> <button id=”add”>Add</button> <button id=”remove”>remove</button> css #foo{ background-color: red; } #foo.green{ background-color: green; } js function addClassNameListener(elemId, callback) { var elem = document.getElementById(elemId); var lastClassName = elem.className; window.setInterval( function() { var className = elem.className; if (className !== lastClassName) { callback(); lastClassName =…