Let's say that we have a #container
like this
<div id="container">
<div class="foo">Foo</div>
<div class="bar">Bar</div>
<div class="baz">Baz</div>
</div>
And somewhere in your app
import { handleEvent, handleEventOnce, triggerEvent } from 'dom.js'
const element = document.getElementById('container')
// Anytime you need
const handleClick = handleEvent('click', {
onElement: element,
matchSelector: '.foo',
withCallback: (event, target) => {
// Only get called when click on '.foo'.
// Good for event delegation.
console.log('Clicked on', target)
}
})
// Anytime you want to remove it
handleClick.destroy()
handleEventOnce
do the same work as handleEvent
. But get called only one time. So you don't need to call destroy
.