Case: I have 5 containers inside vbox layout in a parent container. I attach listeners for tap event on each of the 5 child containers. Another way proposed is to add one event listener on the parent container, because apparently it lightens the DOM.
Questions:
Do Event Listeners take space in DOM?
If I put the event listener on the parent container only, how do I know which child container was clicked?
Event listeners do not take up space in the DOM. They will take up some space in memory. If you can use just 1 listener then you will get a small performance increase over using 5 listeners.
Event listeners do not take up space in the DOM. They will take up some space in memory. If you can use just 1 listener then you will get a small performance increase over using 5 listeners.
jQuery('.wrapper').on('click', function(event) {
var wrap = jQuery(this);
// wrap is now the jquery object of the wrapper
var el = jQuery(event.target);
// el is now the jquery object that was clicked on
if (el.is('#mydiv')) {
// do something here if the item clicked is #mydiv
alert('this is #mydiv');
}
if (wrap.hasClass('elementclass')) {
// do something here if the wrapper of the item clicked has class of 'elementclass'
alert('this is inside .elementclass');
}
});