how to listen to any of multiple elements?

I got it! :tada:

When I first put this together, I used just <img > to create an image element. While this threw me off initially, it also led to the answer (also something Fabian was showing me in another thread): the <img> element wasn’t working with id as I was expecting. Wrapping the <img> in <div> allowed me to access it with .querySelectorAll().

It took a while, but I also figured out how to loop through each <div> and assign an .onclick event to it:

page_navigation.querySelectorAll("#link1, #link2, #link3").forEach(div => 
    div.onclick = (e) => {
let random_image = image_list[Math.floor(Math.random()*Object.keys(image_list).length)]
  image_view.querySelector("#image").src = random_image
    })

Now it works! :slight_smile: The function returns ‘undefined’, but the behaviors are in effect!

Thanks for the time and encouragement!

2 Likes