I
I
Ivan Dontsov2020-05-03 21:54:18
JavaScript
Ivan Dontsov, 2020-05-03 21:54:18

How to apply a function to all divs with id/class?

It is necessary that when clicking, moving and releasing on the div, different functions occurred.
There are a lot of divs with the same id/class and you need to have the same functions for each.
tried but addEventListener only works with 1 div

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Klimenko, 2020-05-03
@MelDe

function handler(e) {
    // ...
}

document.addEventListener('event-type', event => {
    if (event.target.classList.contains('my-class')) {
        handler(event);
    }
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question