After reading about optimally-performing JavaScript, I learned it's best to minimize interaction with the DOM, and rely more on the logic within JavaScript itself.
Part of this is using a single event listener for a list of elements and reading the click target, rather than an event listener for each and every one
ul#menu
li#one One
li#two Two
li#three Three
$ul = document.getElementById('#menu')
$ul.addEventListener('click', function(e) {
if (e.target.id == ...) { ... } // You get the idea
})
I have a website that has several navbars, several dropdown buttons and such. So why not create a single event listener on the body and just look at the event target?
document.body.addEventListener('click', function(e) {
if (e.target.id == ...) { ... };
})
I can't tell what's wrong with this solution from a technical perspective. Something just seems too heavy to me, it has a code smell to it.
Would this be problematic on mobile? Does the nature of <body>
being high in the DOM end up doing more damage to performance than good?
I'm not using jQuery, please suggest pure JS solutions.