0

I'm trying hard to convert one instruction in jquery to vanilla JS, but no luck.

Original jquery: select multiple classes . Two of them with chid elements. And then listen to a click

$(".submit_params, .filter_topics li, .filter_authors li, .clean_params").on("click", function(event){ ..... })

I tried things like:

document.getElementsByClassName("submit_params filter_topics+li filter_authors+li clean_params").addEventListener('click', function(event){ ... }

And

document.querySelectorAll(".submit_params, .filter_topics li, .filter_authors li, .clean_params").addEventListener("click", function(event) { ... }

In both cases: ERROR -> Uncaught TypeError: document.getElementsByClassName(...).addEventListener is not a function.

UPDATE: Thank you jabaa! Your comment solves the issue, like this:

var my = document.querySelectorAll(".submit_params, .filter_topics li, .filter_authors li, .clean_params");
my.forEach(function(el) {
  el.addEventListener("click", function(event) { ... })
})

Thank you!

jaumetet
  • 86
  • 7
  • 1
    `document.getElementsByClassName` and `document.querySelectorAll` return an array-like collection of elements. You have to iterate the list and call `addEventListener` for each element – jabaa Oct 26 '22 at 21:28

0 Answers0