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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:41:16+00:00 2026-06-08T10:41:16+00:00

I am new to JavaScript and I am trying to make two different images

  • 0

I am new to JavaScript and I am trying to make two different images change when the user clicks a button. I got the first image to change, but now I don’t know how to have the next image change at the same time…

It works like this: Both starts as blue. After mouse-click, the left image will display the following sequence (ballblue, ballred, ballred…) the right image on the same button click will display the following (ballred, ballblue, ballred…).

I got this to work, but now how do I make the second image change in a different order (starts out as ballblue.gif, then on button click to ballred.gif, then ballblue.gif, and finally ballred.gif?

Here is my code so far:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head><title></title></head>
<script type="text/javascript">
imgsleft=Array("ballblue.gif","ballblue.gif","ballred.gif","ballred.gif");
var x=0;

function lampSwitch()
{
    document.getElementById("left").src=imgsleft[++x];    
    if (x==3) {
        x=-1;
    }
}
if (!imgs[x+1]) {
    x=-1;
}
</script>
<body>
    <img src="ballblue.gif" id="left" alt="alttext" height="12" width="12"/>
    <img src="ballblue.gif" id="right" alt="alttext" height="12" width="12"/>
    <form action="#">
        <p><input type="button" value="Switch" onclick="lampSwitch()" /></p>
    </form>
</body>
</html>
  • 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-08T10:41:18+00:00Added an answer on June 8, 2026 at 10:41 am

    Code for alternating color:

    <html>
    <head><title>Lamp Switch</title></head>
    <script type="text/javascript">
    imgsleft=Array("ballblue.gif","ballred.gif");
    var x=0;
    
    function lampSwitch(){
        if(++x % 2){
            document.getElementById("left").src=imgsleft[0];
            document.getElementById("right").src=imgsleft[1];
        }else{
            document.getElementById("left").src=imgsleft[1];
            document.getElementById("right").src=imgsleft[0];   
        }
    } 
    </script>
    <body>
    <img src="ballblue.gif" id="left" alt="alttext" height="12" width="12"/>
    <img src="ballblue.gif" id="right" alt="alttext" height="12" width="12"/>
    <form action="#">
    <p><input type="button" value="Switch" onclick="lampSwitch()" /></p>
    </form>
    </body>
    </html>
    

    Code for your specified pattern:

    Both starts as blue.

    Below is the pattern after button click

    left: blue red red…blue red red…blue red red (and repeats the same pattern)

    right: blue red blue red blue red blue …..

    <script type="text/javascript">
    imgsleft = Array("ballblue.gif","ballred.gif");
    pattern = Array("ballblue.gif","ballred.gif","ballred.gif");
    var x=0;
    var y=0;
    function lampSwitch(){
        if(++x % 2){
            document.getElementById("right").src=imgsleft[1];
        }else{
            document.getElementById("right").src=imgsleft[0];   
        }
    
        y++;
        if(y == 3){
           y = 0;
        }
        document.getElementById("left").src=pattern[y];
    } 
    </script>
    
    • 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 make an form for my
I'm quite new to JavaScript andI'm trying to do the following: I've two webpages
I'm relatively new javascript, but I am comfortable in other languages. I'm trying to
I'm completely new to javascript, but I'm trying to create a beta form for
I'm completely new to the javascript and ajax world but trying to learn. Right
I'm trying to make a new spine javascript app using d3 (generated using Spine.app).
I have been trying to make a script that compares two images in HTML5
I'm new to Javascript and I'm trying to make the following code scan to
I am very new Javascript and am messing around trying to make a simple
Possible Duplicate: Open url in new tab using javascript I'm trying to make it

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.