I have a query that I need some assistance with. I've developed a random image generator which is working fine so far:
var randPics = document.querySelector("#randPics");
var getPics = document.querySelector(".getPics");
getPics.addEventListener("click", function(){
//array to store images for the random image generator
var picsGallery = new Array();
picsGallery = ["https://static2.jetpens.com/images/a/000/026/26648.jpg?mark64=aHR0cDovL3d3dy5qZXRwZW5zLmNvbS9pbWFnZXMvYXNzZXRzL3dhdGVybWFyay5wbmc&markalign64=dG9wLHJpZ2h0&markscale=19&s=938428f6eca690069a86f66d0754444b",
"http://assets.sajiansedap.com/media/article_image/cover/large/25505-cara-memilih-nanas-yang-matang.jpg",
"https://cdn.shopify.com/s/files/1/1030/8703/products/epal-hijau-green-apple-each-sebiji_1024x1024.jpg?v=1487817043"]
//generate random no to select the random images
var rnd = Math.floor(Math.random() * picsGallery.length);
//change the pics locations of the source
randPics.src=picsGallery[rnd]
});
#randPics{
width: 300px;
height: 300px;
align-content: center;
}
<body>
<p>Display a random image each time the buttons is clicked!</p>
<p> You get a <span id="text"></span> </p>
<button class="getPics"> Click ! </button>
<br>
<img id="randPics" src="https://d30y9cdsu7xlg0.cloudfront.net/png/45447-200.png">
</body>
Upon clicking the button, a random image from the array will be displayed. However, I am facing an issue with associating text with the images. For example, when the user clicks the button and gets an image of a pen, the text should change to:
You get a
should change to
You get a pen.
Your insights would be greatly appreciated!