Created
April 10, 2020 22:22
-
-
Save tomhodgins/b1f0aacce4217af610e93dc5f79e221d to your computer and use it in GitHub Desktop.
Replace on:click="reference" to addEventListener('click', reference) if given a DOM node and an object of named functions like {reference: event => alert(1)}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function onAttributes(node = document.createElement('span'), functions = {}) { | |
if ( | |
node.attributes | |
&& node.attributes.length | |
) { | |
Array.from(node.attributes) | |
.filter(({name}) => name.startsWith('on:')) | |
.forEach(({name, value}) => { | |
node.addEventListener( | |
name.replace(/^on:/, '').toLowerCase(), | |
functions[value] | |
) | |
node.removeAttribute(name) | |
}) | |
} | |
return node | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment