I have a three divs each with class "card". I want to perform a function when a user clicks any of the divs with this class. Simply, I can use onclick on each of the divs (and it works also) but it is rather a lengthy wok. So I ended up with this:
document.getElementByClassName("card").addEventListener("click", redirect);
But after some research I came to know that getElementByClassName returns a collection and we have to use index values to access each element with it, I finally ended with this:
var card = document.getElementByClassName("card");
for (var i = 0;i < card.length;i++){
card[i].addEventListener("click", redirect);
}
But it is definitely not working.
I know that this is one of the most common problems, but I am not satisfied with any of the answers to related questions in the context of my problem.