Skip to content
Advertisement

Prevent click event being triggered on children elements

I don’t understand why when I add a click event listener on an element, its children triggers it too.

I want that the parent is triggered even if the children are clicked, which should be the normal behavior I think.

Here’s the code :

JavaScript
JavaScript

Advertisement

Answer

You can use e.target to check what has been clicked, then for example check if the jobsList collection contains the target element. If it does then one of the .job elements is the target, otherwise the target is a child element.

JavaScript

https://jsfiddle.net/4r7hLua2/

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement