Internet Explorer 9, 10 & 11 Event constructor doesn’t work

There’s an IE polyfill for the CustomEvent constructor at MDN. Adding CustomEvent to IE and using that instead works. (function () { if ( typeof window.CustomEvent === “function” ) return false; //If not IE function CustomEvent ( event, params ) { params = params || { bubbles: false, cancelable: false, detail: undefined }; var evt … Read more

JavaScript: Listen for attribute change?

You need MutationObserver, Here in snippet I have used setTimeout to simulate modifying attribute var element = document.querySelector(‘#test’); setTimeout(function() { element.setAttribute(‘data-text’, ‘whatever’); }, 5000) var observer = new MutationObserver(function(mutations) { mutations.forEach(function(mutation) { if (mutation.type === “attributes”) { console.log(“attributes changed”) } }); }); observer.observe(element, { attributes: true //configure it to listen to attribute changes }); <div … Read more

tech