I am trying to create a page that allows viewers to view a new image every time the user clicks on it. The images are named 0.jpg, 1.jpg, 2.jpg, 3.jpg, and 4.jpg.
However, the image URL never changes upon clicking. I also tried analyzing the image in Chrome Developer tools, and the console issues no errors. Why is that?
Here is my javascript.
var imageCount = 5;
var currentIndex = Math.floor(Math.random() * imageCount);
function loadNewPhoto() {
currentIndex = (currentIndex + (Math.random() * 3) + 1) % imageCount;
alert(currentIndex);
document.getElementById('generatedImage').src = 'images/' + currentIndex + '.jpg';
}
// Shuffle order of photos.
document.onready = function() {
loadNewPhoto();
document.getElementById('getMeNewPhotoLink').onclick(function(e) {
e.preventDefault();
loadNewPhoto();
});
}
and HTML
<a href="#" id="getMeNewPhotoLink">
<img id="generatedImage" src="images/0.jpg" alt="" />
</a>
Got it working here is your DEMO
New HTML:
hrefis now a javascript function instead of"#", a more elegant solutionNew Javascript: