A
A
Alexey Nikolaev2018-03-23 20:41:26
JavaScript
Alexey Nikolaev, 2018-03-23 20:41:26

How can you catch a popup event in the middle of its path?

Good evening.
There is a table row, on clicking on it, a click event occurs. I would like to catch this event in the handler on the document in order to capture those elements that will be added dynamically. In jQuery I would do this:

$(document).on('click', '[data-behavior="something"]', event => {
    // Все работает
});

But in pure JS it's more difficult. For an event object, if it is added via document.addEventListener, event.target will be equal to either the cell or what lies in it, but not the table row, and currentTarget, of course, is equal to document. If you add events to the nodes of the desired selector, then the automatic event binding to new elements will disappear. How to do it without crutches? How did the jQuery authors do it - does it work for them? What is the algorithm?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2018-03-23
@Heian

How did the jQuery authors do it - does it work for them?

Climb the DOM tree from event.target to the element to which the handler is hung, check the encountered nodes for compliance with the selector. Interested in the details - open the jQuery code, and take a look.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question