Basically I want to do something like this:
<my-root-element id="myID">
<button id="some-id">Occupy The Universe</button>
<div id="x" class="class-1 class-1a">
<div id="y" class="class-3">
<p>Rabit hole</p>
</div>
</div>
</my-root-element>
let rootEl = document.getElementById(myID);
rootEl.addEventListener('click', function(e) {
if (e.target && e.target.matches('button#some-id')) {
console.log("Did something nasty");
} else if (e.target && e.target.matches('.class-1.class-1a') || e.target && e.target.matches('.class-1.class-1a > .class-3')) {
console.log("Was there");
}
});
It looks for me that class nesting is not working, but maybe I'm missing something? I would like to do this in one IF as both things do the same thing. It looks like querySelector
supports nested classes, but can't figure out, how to implement it there.
if ((e.target && e.target.matches('.class-1.class-1a')) || (e.target && e.target.matches('.class-1.class-1a > .class-3'))) {
– Heretic Monkey&&
binds more tightly than||
. – Pointye.target
test just once and then parenthesize the||
subexpression. However all of that is irrelevant if the selectors are incorrect anyway :) – Pointy