When attempting to add a "click" event listener to a single element, it functions correctly:
var blog1 = document.getElementById("b1");
blog1.addEventListener("click", function(){
window.location.href="blog1.html";
});
However, when I try to use a for loop to assign click listeners to multiple elements at once in the following manner:
var blog1 = document.getElementById("b1");
var blog2 = document.getElementById("b2");
var blog3 = document.getElementById("b3");
for(var i = 1; i < 4; i++){
var k = ("blog" + i);
k.addEventListener("click", function(){
window.location.href = (k + ".html");
});
}
I encounter the error:
TypeError: k.addEventListener is not a function
Could someone please help me identify where my mistake lies? Thank you so much. P.S- Attempting to achieve this using vanilla JS.