DEV Community

R☭
R☭

Posted on

1 2

Javascript events

Working on a message system, i want to catch all the events that happen on the message element, or its children. Would this be the most optimal way of doing this?

I can also add a pointer-events: none through CSS on all the child elements. But that doesn't feel like a good scalable way to go.

document.addEventListener('click', function(event) {
    for (var i = 0; i < event.path.length; i++) {
        if (event.path[i].classList && event.path[i].classList.contains('message'))         
        {
            doThings();
            break;
        }
   }
});
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs