Skip to content
Advertisement

Is there a way to simplify the detection the index number of a button with the same div class and load it’s corresponding image?

So I managed to get the images I set under the same class to load up their respective fullsized images when clicked on.

However, I remembered seeing there was a way to simplify it further?

This is what I have so far, that is working out fine, but just needs to be simplified for convenience:

JavaScript

Thanks mates!

Advertisement

Answer

Use the forEach() method when working with an array of a class. I have provided a simplified method for your code using forEach() methods.

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement