I am trying to make a gallery.
I succeeded in all the steps except the final one when clicking on a thumbnail would change a text in a paragraph into another text that I put in an array.
While I'm inside a for-loop, I want the text of the index 2 to show when I click the image of the index 2. I seem to have followed the right syntax, but still the paragraph shows "undefined" as a result. However when I choose an index, like whichever picture I click, I want to show the text of the index 3, it works.
Here is my simplified code that should allow me to show a text while clicking on a paragraph. It would be awesome if you guys didn't use jQuery. Thank you ALL!
<!DOCTYPE html>
<html>
<body>
<p class="demo">1</p>
<p class="demo">2</p>
<p class="demo">3</p>
<script>
var cars = ["Saab", "Volvo", "BMW"];
var para = document.getElementsByClassName("demo");
for (k=0;k<cars.length;k++){
para[k].onclick=function(){
this.innerHTML = cars[k];
}
}
</script>
</body>
</html>