henry henry - 7 months ago 12
Javascript Question

Is there a way to listen to the triggering of an event listener of a child element?

Let's say we have a parent div. Is there a way to handle the triggering of a certain event, let's say click event in this case, of any of the child elements, let's say a link () in jQuery?

Answer

Depending on the type of event, you should be able to use either the click() or on() events to listen for it using your preferred syntax :

// Pick up any click events triggered by a "child" element below a specific parent
$('.parent .child').click(function(){ ... });

or :

// Do the same thing (using .on() will pick up dynamically added elements)
$('.parent').on('click','.child',function(){ ... });

Example

You can see a complete example of this here and demonstrated below :

enter image description here