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 8666207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:40:15+00:00 2026-06-12T17:40:15+00:00

I am fairly new to JavaScript, though not to programming. My exercise is to

  • 0

I am fairly new to JavaScript, though not to programming. My exercise is to have a page that shows an image and allows the user to go to next/previous image by clicking on a button. I want to load the next/previous image from the server on the button click, so the page doesn’t have to load all the images into the browser at once.

This version just has a button to go to the next image, and I’m trying to load it with jQuery or AJAX. The page loads the initial “image0”, and the JavaScript debugger shows that the onClick event executes and does what I would expect, but the image doesn’t change.

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script>
var imageIndex = 0;
var jpgImageName = "image0"
var loadString = "images/" + jpgImageName + ".JPG";

function changeImage()
{
  imageIndex++;
  if (imageIndex > 2) { imageIndex = 0; }
  var locationString = "images/image" + imageIndex + ".JPG";
  var loadString = $("<img />").attr('src', locationString);
  $("imageSlot").append(loadString);
}

</script>
</head>
<body>

<div id="imageSlot">
  <img id="currentImage" src="images/image0.JPG" height="400" alt="image"+imageIndex/>
</div>
<button id="changeImage" onclick="changeImage()">Change Image</button>

</body>
</html>

I got the append function from another Stack Overflow example. I want to replace the current image, and have seen a suggestion to use html() instead of append() for that, but figured I’d try to get this working before I started changing the example.

I’ve also tried a .load() function on the image itself with just the URL for the next image. That didn’t work either, though attempting to show both in the above example seemed likely to confuse people reading the question. But if you want to show me how to solve it that way, or both ways, that’d be great.

  • 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-12T17:40:16+00:00Added an answer on June 12, 2026 at 5:40 pm

    If you are wanting to replace the current image with the next one, then you could try just changing the src of the current image directly:

    function changeImage()
    {
      imageIndex++;
      if (imageIndex > 2) { imageIndex = 0; }
      var locationString = "images/image" + imageIndex + ".JPG";
      $('#currentImage').attr('src', locationString);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm fairly new to JavaScript and I'm trying to access a page that requires
I have a fairly simple page that displays an image in a pop-up window.
First let me preface this question by saying that I'm fairly new to Javascript.
I'm still fairly new to javascript and I'm not finding where I'm making my
I'm fairly new to facebook development, but have experience with PHP and JavaScript. I've
I'm fairly new to Javascript/JQuery. I'm trying to write an app that takes text
I have the following (fairly) simple JavaScript snippet that I have wired into Greasemonkey.
I'm fairly new to javascript and I'm trying to make an form for my
Fairly new to programming. I just can't wrap my head around how to get
Being fairly new to programming, I am having trouble understanding exactly what Homebrew does...

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.