There is such a code and it works:
function openLikeBlock() { var like = document.getElementById("like"); like.addEventListener('click', function (e) { e.preventDefault(); if (!this.classList.contains('open')) this.classList.add('open'); else this.classList.remove('open'); }); }
But if you add a ClassName, then there will be an error:
var like = document.getElementsByClassName("like");
Uncaught TypeError: like.addEventListener is not a function
I need exactly the class, since there are several such blocks on the page (therefore I use this
)
Question: how to do, so that I could receive an element by class, and not ID?