0

I have a problem that i cannot resolve.

I'm adding a click event listener to a parent container and also to its child.

I need to take the info of both clicks but the problem is when the child container is clicked, the click event from the parent is also triggered.

I do not want that to happen. So, does anyone know how to avoid that behavior?

I want to record just when it is clicked in one or the other. Not in both at the same time. So far I've been trying with this options:

let validar = false;

parentCont.addEventListener('click',()=>{
   validar = true;
   pointControl();
});

childCont.addEventListener('click',()=>{
   validar = false;
   pointControl();
});

Also with the simple option:

parentCont.addEventListener('click',function);
childCont.addEventListener('click',function);

When the parent is triggered, the child obviously not. But when the child is triggered, the parent also is.

I tried with other things but also didn't work, maybe I'm doing something wrong. Anyway, thanks!

tobi
  • 47
  • 6
  • This is super google-able but as a fellow lazy person - i will spare you - in the child function...put event.stopPropagation() to stop the event from bubbling up to the parent – Ryan Sep 26 '22 at 02:35
  • hahahah thanks! @Ryan, for the record i must say that my knowledge in js is basic and at this point yes, i google it, but when you don't have the knowledge you don't search well! thank you again! – tobi Sep 26 '22 at 02:45

0 Answers0