4 ms·
I reach out for HTML events instead of addEventListener all the time. I get free event addition when dynamically adding content, also removal and tidying up aft
by phillipseamore 4y ago
I reach out for HTML events instead of addEventListener all the time. I get free event addition when dynamically adding content, also removal and tidying up after elements are removed - no memory leaks from unused event listeners. Plus it's obvious from browsing the code what different elements are hooked up to. I get called out on this often, but no one can argue with the ease of use and performance.
- chris37879 4y agoI prefer them too for the same reasons, they're even available with a really similar syntax in React's JSX extension: <button onClick={onClickHandler}> I'm a big fan of the data attributes for the same reason, they bind onto the actual element they belong with, they're available in JS and CSS, and they add semantic meaning to your markup when you're reading the source.
- asddubs 4y agothey're also much nicer for user-editable templates if you expect your users not to all be experts, or even if they are, have a full grasp of the system. they're right there where they're relevant, you see the element and right away you know that javascript will happen when you interact with it in certain ways