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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:21:57+00:00 2026-05-30T13:21:57+00:00

I am writing this in JavaScript, and the issue is that I can’t seem

  • 0

I am writing this in JavaScript, and the issue is that I can’t seem to figure out how to make a permutation. Ideally, I want to click the button and image would change to next, from a1.jpg to a2.jpg to a3.jpg to a4.jpg to a1.jpg and so on… But when I wrote this code, it would skip a2 and a3. I also did rearrange the conditionals since I realized that it reads from the top to bottom, but it would always skip one of the four pictures.

function changeImage() {
     if (document.images) {
          if (document["buttonOne"].src == a3.src){
          document["buttonOne"].src = a4.src}
     }
     if (document.images) {
          if (document["buttonOne"].src == a2.src){
          document["buttonOne"].src = a3.src
     }
     if (document.images) {
          if (document["buttonOne"].src == a1.src){
          document["buttonOne"].src = a2.src}
     }
     if (document.images) {
          if (document["buttonOne"].src == a4.src){
          document["buttonOne"].src = a1.src}
     }
     }
  • 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-05-30T13:21:58+00:00Added an answer on May 30, 2026 at 1:21 pm

    Try it in a more simple way

    var imgs = ['a1.jpg', 'a2.jpg', 'a3.jpg', 'a4.jpg'];
    
    function changeImage() {
        var img = document["buttonOne"];
        if (!img._index) img._index = 0;
        img.src = imgs[img._index++];
        if (img._index >= imgs.length) img._index = 0;
      }
    

    img._index replaces the global variable with index of the shown image. Good to use when you want to change a few images, each of them will keep its own counter.

    ps: if the only thing that changes is the number in the filename then the code can be even shorter without an array:

       function changeImage(max) {
            var img = document["buttonOne"];
            if (!img._index) img._index = 0;
            img.src = 'a' + img._index + '.jpg';
            if (img._index >= max) img._index = 0;
          }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a little JavaScript tool for myself that I plan on making available
I am a bit new to Javascript. I am writing this dummy script to
My issue is that sometimes a piece of JavaScript (often Google Analytics) may take
I want to write to a text file using javascript. I know that it
I'm writing this question in the spirit of answering your own questions, since I
I'm writing this question from the standpoint of an ASP.NET application. However I realize
I am writing this java program to find all the prime numbers up to
I'm writing this question with reference to this one which I wrote yesterday. After
Unfortunately I am not writing this question from my Developing PC so I might
I am writing this comment class: class Comment { public $id; public $post_id; public

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.