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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:40:58+00:00 2026-06-13T07:40:58+00:00

I wanted to implement a swapImage on my site that would swap one image

  • 0

I wanted to implement a swapImage on my site that would swap one image on mouseover, and another image on mouseout. Essentially, with the solution I found I could also probably swap different images for mousedown and mouseup even.

I have this happening in 8 different nested tabs, all of which are on the same page.

The javascript looks like this:

<script type="text/javascript">
    function swapImageFiat(imgID, imgSrc) {
        var theImage = document.getElementById(imgID)
        theImage.src = imgSrc;
    }

    function swapImageHarley(imgID, imgSrc) {
        var theImage = document.getElementById(imgID)
        theImage.src = imgSrc;
    }

    function swapImageHotWheels(imgID, imgSrc) {
        var theImage = document.getElementById(imgID)
        theImage.src = imgSrc;
    }

    function swapImageVoltron(imgID, imgSrc) {
        var theImage = document.getElementById(imgID)
        theImage.src = imgSrc;
    }

    function swapImageBenchmade(imgID, imgSrc) {
        var theImage = document.getElementById(imgID)
        theImage.src = imgSrc;
    }

    function swapImageCrew(imgID, imgSrc) {
        var theImage = document.getElementById(imgID)
        theImage.src = imgSrc;
    }

    function swapImageReuters(imgID, imgSrc) {
        var theImage = document.getElementById(imgID)
        theImage.src = imgSrc;
    }

    function swapImageMarsVolta(imgID, imgSrc) {
        var theImage = document.getElementById(imgID)
        theImage.src = imgSrc;
    }

    function preLoadImages() {
        for(var i = 0; i < arguments.length; i++) {
            var theImage = new Image();
            theImage.src = arguments[i];
        }
    }
</script>

And the inline code for each image would be essentially this:

<img class="diagram" src="img/fiat.gif" id="imgToSwapFiat" alt="Diagram" data-title="Fiat Diagram" onClick="swapImageFiat('imgToSwapFiat', 'img/fiat-animated.gif')" onmouseout="swapImageFiat('imgToSwapFiat', 'fiat.gif')" height="189" width="358" />  

I would like to be able to use less IDs, less of the same repetitive script, and shorter inline code.
But I also want the flexibility of being able to just change the images and their IDs, rather than fussing with JQuery or other script. This is why I’m using getElementById.

Is there a more efficient way for me to write that JavaScript, and/or Inline code?

Also

Is there a way for the image to swap back to the original after the animation stops playing, rather than on mouse-out?

  • 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-13T07:41:00+00:00Added an answer on June 13, 2026 at 7:41 am

    Yeah, sure there is. As Jan mentions – all of your functions are identical. There’s no need to duplicate them all. Also, you can remove the inline event-handler code from your html, simply attaching the mouseover/mouseout or mousedown/mouseup.

    Simply add a new attribute to each image that you wish to have the effect for, then runs some js on page-load to attach the handler.

    Here, I’ve just used (and checked for the existance of) custom attributes. If the image has them, I attach handlers. If not, it’s no different to any other normal image. This should be nice an re-usable and little effort to use.

    As for resetting the image once the animated gif has played, I’d probably look at Jan’s first suggestion. A slight modification on the idea would be to make the last frame transparent, then simply position the animated image over (z-index higher) the static one.
    When it’s done, you’ll see the original image again. You’ll have to decide on a system for refreshing these images, should they need to be re-played. I’m not sure, perhaps setting their src to ”, before setting it back to the animated.gif would cause the animation to play from the start again – you’d have to check if to decided to attempt such an approach.

    <!DOCTYPE html>
    <html>
    <head>
    <script>
    function byId(e){return document.getElementById(e);}
    
    function mInit()
    {
        var imgList = document.getElementsByTagName('img');
        var i, n = imgList.length;
        for (i=0; i<n; i++)
        {
            if (imgList[i].hasAttribute('img2'))
                imgList[i].addEventListener('click', imgClick, false);
            if (imgList[i].hasAttribute('img3'))
                imgList[i].addEventListener('mouseover', imgHover, false);
            if (imgList[i].hasAttribute('img4'))
                imgList[i].addEventListener('mouseout', imgOut, false);
        }
    }
    
    // because these function are attached with addEventListener, the 'this'
    // keyword refers to the image element itself. No need for IDs..
    function imgClick()
    {
        this.src = this.getAttribute('img2');
    }
    function imgHover()
    {
        this.src = this.getAttribute('img3');
    }
    function imgOut()
    {
        this.src = this.getAttribute('img4');
    }
    
    
    window.addEventListener('load', mInit, false);
    
    </script>
    <style>
    </style>
    </head>
    <body>
        <img src='img/gladiators.png'>  <!-- this image won't be touched, as it doesn't have img1, img2 or img3 attributes -->
        <img src='img/opera.svg' img2='img/chromeEyes.svg' img3='img/rss16.png' img4='img/girl2.png'/>
    </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently I wanted to implement a printf wrapper. After some search I found the
If I wanted to implement a JDBC Driver, how would I know which interfaces/abstract
What would you say if a developer wanted to implement a sql2008 dev environment,
Let's say I had a fresh MVC site and I wanted to implement URLs
I wanted to implement Chrome's Omnibox search into my site, and followed the relatively
Alright, another interesting problem over at Route 50. We wanted to implement a true
I wanted to implement an app that prevents calls like a firewall. When I
I wanted to implement my own debug function that has the same signature as
So I wanted to implement Timer in my Anroid program and I found out
I wanted to implement a multi-select AutoCompleteExtender that displays a checkbox on each row.

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.