Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8245239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:07:05+00:00 2026-06-07T22:07:05+00:00

I am trying to create a page that allows viewers to view a new

  • 0

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>​

http://jsfiddle.net/qLQZf/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T22:07:08+00:00Added an answer on June 7, 2026 at 10:07 pm

    Got it working here is your DEMO

    New HTML:

    <a href="javascript:shuffle()" id="getMeNewPhotoLink" >
        <img id="generatedImage" src="images/0.jpg" alt="" />
    </a>
    

    href is now a javascript function instead of "#", a more elegant solution

    New Javascript:

    var imageCount = 5;
    var currentIndex = Math.floor(Math.random() * imageCount);
    alert(currentindex);
      function loadNewPhoto() {
        currentIndex = Math.floor((currentIndex + (Math.random() * 3) + 1) % imageCount);//Added Math.floor, else was generating decimal value
        alert(currentIndex);
        document.getElementById('generatedImage').src = 'images/' + currentIndex + '.jpg';
      }
    
      // Shuffle order of photos, no need of preventdefault, onclick and ready event handling via code.
    
      function shuffle() {
         loadNewPhoto();
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a page that allows a user to change the "look
I'm trying to create an ASP.NET/C# page that allows the user to edit a
Trying to create a page that allows users to add edit and view a
Im trying to create a page that allows a user to select a maximum
I am trying to create a login page that will send the user to
I am trying to create a search function that allows the user to search
I am trying to create an application that allows the user to search a
I am using FOSUserBundle and I am trying to create a page that allows
As the title says, I'm trying to create a web page that allows admins
I'm trying to create a page that contains a grid and searching. The issue

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.