Our goal is to make simple things simpler... right?
Simple Swap is a way to use custom tag attributes and JavaScript triggers to simplify image rollovers, and make Image Rollovers the easiest thing in the world to use without messy JavaScript function calls and image preloading.
Peter-Paul Koch gets us heading down the path of using custom attributes of existing HTML elements to help abstract our Javascript and keep it from cluttering our HTML in his article JavaScript Triggers. This article uses that method to simplify image rollovers.
We have all dealt with JavaScript files pre-loading images for mouseover and mouseout events, and some ugly cryptic functions (not to mention HTML) to execute image rollovers. What we needed was just a easier simpler way to put that all together.
Let's work to apply this concept abstraction, and see where it leaves us. Conceptually, Peter-Paul argues that we should add an attribute to the html tag which triggers Javascript actions. Our starting place is a simple image embeded in a page.
The only additional piece of information we need for a rollover image, is where the image is. So we add that specific image source as a custom attribute named oversrc to the image. This attribute will be what triggers our javascript rollover later on.
Before we get to triggering image rollovers based upon this attribute, we need to setup a function which will execute the onmouseover and onmouseout events. This function performs both the mouseover and mouseout events, and just changes which image url to set the src attribute to. It's a simple function, but so are image rollovers.
This approach is simple, and is just one step ahead of where we are; it ties the rollover image source url with the tag, and simplifies our rollover functions.
Now, if we didn't want to trigger our javascript automatically we could embed it like this:
This works, however our goal is to have this rollover functionality triggered by the existance of the oversrc attribute on an image tag, so we need to automagically create the onmouseover and onmouseout function calls.
This function simply goes through all images on a page, and if the oversrc attribute is present, it adds the appropriate event handlers for the SimpleSwap rollover function. The only tricky part is setting the scope of the function call when adding it as an event handler so it gets the correct value for 'this' when passed as a parameter to the SimpleSwap function.
Now one last piece of code to run the setup function when the document onload event is fired. We want to do this in such a way that we don't need to add a function call to our <body> tag onload event handler. This method of attaching the SimpleSwapSetup function preserves any existing functions on that event handler.
The following code is all we end up with for a nice clean implementation of Image Rollovers:

A final example is here: http://jehiah.com/sandbox/simpleswap.html
Download the final javascript file : http://jehiah.com/download/simpleswap.js
6/10 - version 1.1 - Major updates to simplify javascript code (not usage) even further provided by Adam Vandenberg. Combined SimpleSwapOn and SimpleSwapOff and simplified some of the inialization code.
4 years, 6 months ago
Holy cow that’s friggin’ rad. I don’t use image rollovers much anymore, but wow, is this the way to do it when it needs doin’.
4 years, 6 months ago
Two simple changes to make it even better:
1) There is no language attribute to . User type=”text/javascript”
2) Drop the “javascript:” strings from the eventhandlers. They are not necessary to indicate javascript, eventhandlers all contain script. “javascript:” will just be interprated as a label by the Javascript engine.
4 years, 6 months ago
Just a quick note, if anybody’s interested: This script works excellently with alpha PNG transparencies in IE6 with Dean Edwards IE7 script (which, if you haven’t checked it out, you really should; quite amazing).
Again, great work. Thanks Jehiah.
4 years, 5 months ago
Thanks so much for this, it’s great and could not be simpler to implement!
4 years, 5 months ago
Does this script take into consideration that images may not be loaded yet?
4 years, 5 months ago
nicolas: good question. The script actually preloads the rollover image so that it displays “instantly”.
Of course if your site is heavy on images; or you are accessing over a slow connection – when mousing over – you see the normal image untill the rollover image has completed loading.
4 years, 5 months ago
Actually, this doesn’t work:
function SimpleSwap(el,which){
el.src=el.getAttribute(which||”origsrc”);
}
It swaps the image, but does not swap it back.
4 years, 5 months ago
@Anonymous: actually it does work… It’s a clever usage of defaults. If you notice only one of the calls to
SimpleSwap()has two parameters.What browser are you using where it isn’t working? example page?
4 years, 5 months ago
IE6 http://www.mapgasprices.com/dev/index.asp
Mouseover on North frame border
4 years, 5 months ago
ahhaa. my intermediate example leaves something out. Since you arn’t calling the
SimpleSwapSetup()onload, you also needorigsrc="/path/to/normal/image.gif"property. This will take the place of the following line of code in the setup function.x[i].setAttribute("origsrc",x[i].src);hope that clears things up.
4 years, 1 month ago
Jehiah,
Great site, great script. I’m currently using it on my (in construction) website for previous and next buttons for an image slideshow. It works great, but I need to not display the images when the viewer is at the beginning or end of the slideshow… Is there a way, in JavaScript, to not display the next button image when the viewer is at the end of the slideshow? I already have a variable that equals 1 when the user is at the end, but I can’t figure out how to essentially remove the img tag when this variable is 1. Is that possible?
4 years, 1 month ago
Hey, I have an idea for you. You can change the style on an element so that it doesn’t display Given the following tag, you would access it by the “
id” attribute, and change the style. To display it again just set the.style.display='';4 years, 1 month ago
Perfect! That works wonderfully… Thanks for your help!
4 years, 1 month ago
I’m having a browser issue with simpleSwap… my mouseovers aren’t working when I view my site with Mozilla, they only work when viewing with IE… is this part of the script or did I miss something?
4 years ago
Truly masterful. This is the best mouseover script I’ve seen, thank you for making it so simple to implement.
I needed a different behavior for one page on my site, so changed mouseover & mouseout to mousedown & mouseup (respectively), do you anticipate any browser compatibility issues for these new triggers?
It works fine in firefox but I don’t have other browsers to test with.
4 years ago
@dcb:
You are welcome.
No browser compatabilities with mousup/down but if it is for something that needs to be used via keyboard as well you might want to experiment with using onfocus/onblur
4 years ago
It’s great and all but how would you add a sound effect to it. (I’ve tryed myself but I’m still trying to get use to javascript)
4 years ago
This is the code I tryed for sound
sound=”navsound0″
4 years ago
what happened to that comment I posted?
4 years ago
Great Script, but I can not get it to work in a <iframe> page. see http://www.eluk.co.uk/upton2006/ for example. It works great with the logo but not on the Welcome panel.
4 years ago
Rollover works fine. I need to change an image and text. The text must be displayed as text not an image. Any suggestions?
3 years, 11 months ago
There is one Problem with the onload function. If you have another onload script in your code the script dont work anymore.
3 years, 11 months ago
Try
[js]
window.attachEvent(”onload”,SimpleSwapSetup);
[/js]
3 years, 10 months ago
Could an onmousedown be used with this script?
The:
window.attachEvent(”onload”,SimpleSwapSetup);
Where would that code be placed?
3 years, 9 months ago
Following code doesn’t work on Firefox
[js]
window.attachEvent(”onload”,SimpleSwapSetup);
[/js]
Sorry don’t know the solution
3 years, 8 months ago
Jehiah,
You seem to know this stuff inside out and backwards so I’m hoping you might be able to help me with a problem that relates to image swapping. I’m kind of at a dead end.
I have a function that swaps out several images on a page. The function is invoked by clicking on a hyperlink. I have this working just fine, but if the user navigates to another page and then uses the browser’s Back button to return to the page where the images were swapped, the original images are shown (and not the images that were swapped). Is there some way to preserve the last swap state?
You can see this in action (and view the source) here:
http://www.designityourselfgiftbaskets.com/wine-gift-baskets-wine-baskets.htm
My problem is that after the products are “filtered” and the images are swapped, the users are clicking on a product and going to the product detail page. When the use the Back button to return to product listing page, the original unswapped images are restored.
Any help would be greatly appreciated!
3 years, 6 months ago
sorry to butt in, but have you thought about using a cookie?
3 years, 6 months ago
Hi! thanks for the great code! a relief to have some meaningful js that i can nut out..
i have adapted this to trigger disjoint rollovers, and have it working in FF and IE6, but the FF JS console warns
‘Element referenced by ID/NAME in the global scope. Use W3C standard document.getElementById() instead.’
.. i havent been able to figure out where the problem lies, think i must be missing something.. tried googling, no definitive solution…
for the moment, i can live with the warnings, but its less satisfying :)
thanks again! regards, tim
[code]
function DJSwap(el,which){
el.src = (which) ? which : el.getAttribute("origsrc");
}
function DisjointSwapSetup(){
// define the image which will change:
var dj = window.document.getElementById("dj");
// save original src
dj.setAttribute("origsrc",dj.src);
// gather the triggers for the rollover
var a = document.getElementsByTagName("a");
for (var i=0;iimg = new Image();
a[i].djsrcimg.src=djsrc;
// set event handlers
a[i].onmouseover = new Function("DJSwap(dj,'" + djsrc + "');");
a[i].onmouseout = new Function("DJSwap(dj);");
}
}
var PreSimpleSwapOnload =(window.onload)? window.onload : function(){};
window.onload = function(){PreSimpleSwapOnload(); SimpleSwapSetup(); DisjointSwapSetup();}
[/code]
3 years, 4 months ago
got the mouseover working, but not when there is a div above it. My buttons have text positioned over them. When you mouse over the text the button doesn’t work. I’m assuming this is because the mouseover action for the div takes precedence. Any workaround?
3 years, 4 months ago
I’m a total novice. Is there a way to edit this code, or, would you recommend a different code were the “_over” image is larger? I want a smaller picture setting on the page, but, a larger one to display on the mouseover.
3 years, 4 months ago
pardon my last question. Duh. I was using a bad file name. This script works wonderfully, especially for someone like me with absolutely no understanding of the script. Thank you very much for providing this code.
3 years, 2 months ago
Great Job! Thanks very much!
2 years, 11 months ago
Thanks for the scripts! I really like this solution, but I was concerned about failed W3c validation due to the custom img attribute. I have worked up a (somewhat untested) modification that uses the Class attribute as a trigger for swappable images, and then uses a naming convention to target the rollover image. The rollover images are named identically to the originals, but have “_on” added before the “.gif”.
[code]
function swapItSetup(){
var x = document.getElementsByTagName("img");
for (var i=0;iimg = new Image();
x[i].oversrcimg.src = x[i].getAttribute("src").replace(".","_on.");
}
[/code]
I have yet to try this in any browser other than FF/Mac, but we’ll see.
2 years, 9 months ago
Can someone show me an example of #33 bugeye’s Class attribute used as a trigger for swappable images? Thanks!
2 years, 9 months ago
WOW! Works like a charm!
no probs in IE nor in FF.
I dont think theres a simpler solution.
Thank you!
2 years, 8 months ago
Hmm…when I roll over in ie6, the hover-image just stays…perhaps because I’m using a png-transparency fix?
2 years, 8 months ago
Jehiah,
Great solution! Instantly solved a problem I was struggling with. Thanks!
Took and combined your solution with an incomplete comment of one of your other readers, and someone else I saw out there to overcome a few issues I had :
1) I wanted to be able to pass validation testing, so no use of the “oversrc” tag
2) I wanted to be able to easily classify certain images as having this rollover functionality, while others did not.
3) I did not want to preclude the use of the Class attribute for other formatting of images.
So a minor variation to your function is as follows:
function SimpleSwapSetup()
{
var images = document.getElementsByTagName(”img”);
for (var i=0; ioff.”))
{
// preload image
// comment the next two lines to disable image pre-loading
images[i].oversrcimg = new Image();
images[i].oversrcimg.src = images[i].getAttribute(”src”).replace(”off.”, “on.”);
// set event handlers
images[i].onmouseover = new Function(”SimpleSwap(this, ‘oversrc’);”);
images[i].onmouseout = new Function(”SimpleSwap(this);”);
// save original src
images[i].setAttribute(”origsrc”, images[i].src);
images[i].setAttribute(”oversrc”, images[i].oversrcimg.src);
}
}
}
with this variation, I simply set the SRC attribute to the original image filename, with an “off” suffix prior to the extension (i.e., src=”myfile.jpg” becomes src=”myfileoff.jpg”), and then add a corresponding “on” suffixed image (i.e., src=”myfileon.jpg”). Thus under normal conditions, the _off image is displayed, and during a mouse-over, the _on image is displayed.
If I don’t want an image to have any rollover capability, I leave out the _off suffix, and the function ignores it.
Hope this is useful to someone else!
2 years, 6 months ago
Great piece of code!
I just have a problem with it. I try to place several swap images one beside another with no space between them as a simple menu. When I use your js it just adds a default space spoiling the side-by-side effect.
Is there any way to add CSS or hspace on these images?
2 years, 5 months ago
oww… Im a beginner and rollover images using codes are one of our projects in school… using html in notepad! so all of those rollover tags really have javascript codes??? Isnt there any code thats only for HTML??? pweeaassee… tell me!… T_T
2 years, 4 months ago
Unfortunatly as per a comment above about Dean Edwards IE7 script working with this… It does NOT.
The image rollover changes fine but does not change back.
Is there any way to tweak this and make it work?
2 years, 3 months ago
This is awesome!!! Thanks for sharing!
2 years, 3 months ago
Yeah, doesn’t work if you’re using a png fix in ie6.
2 years, 3 months ago
This is indeed great. Nice, streamlined and effective, and great for someone who has written nearly no JavaScript in the past 5 years. I have a question, though – what Creative Commons license do you offer this under – Attribution?
Thanks
2 years, 1 month ago
Hello,
thanks for the nice little javascript.
only disadvantage with it is that oversrc is not a valid attribute for img tag.
2 years ago
Anyone else having safari problems?
2 years ago
Thank you for such an elegant script. I’m using it to create a seamless-transition photo portfolio for a friend.
I’m wondering if it’s simple enough to add a double mouseover event- everything’s working perfectly, but I’d like not only to swap out photos on click, but somehow swap out the photo link as well- please take a look at my file in test for clarity.
If anyone has suggestions, I’d appreciate them- my javascript is weak, I don’t know where to begin. Thanks again!
1 year, 12 months ago
I am a web developer HAHA… well I try to be, for this website, and I’ve always wanted to have a function whereby the customer clicks on a picture of the vehicle (in the more detailed page), and that picture swaps to the bigger image on the top left hand side. The problem is, the pictures cannot be preloaded; I use asp coding, and it calls the pictures by rows.
Any suggestions?
1 year, 10 months ago
Hi jehiah, thanks for sharing this script. I am trying to use the it, but for some reason, when I use it more than once on a page, it only works on the last instance… I am trying to figure it out, but to no avail. Any ideas or suggestions?
1 year, 10 months ago
Hi… I figured it out, I goofed. Thanks for writing a great script!
1 year, 8 months ago
Great code. Thank you. Can somebody post the complete code without ‘oversrc’ so that it is W3C standard compatible — I am java script beginner.
1 year, 8 months ago
Hello there,
I’ve been looking fr a script like this for my new website. It’s great and the comments of users have also helped me a lot.
Thing is that… this script is quite complex, long even and takes some time to understand it and get the hang of it.
Until now I’ve been using Frontpage’s swap effect and it created tremendous errors in certain browsers…
Thanks for the script!
1 year, 6 months ago
[...] Note: My idea from this came from Jehiah Czebotar’s SimpleSwap Rollover Method. [...]
1 year, 6 months ago
A simple and elegant solution…. I’ve been looking on and off for something that will work as well as this for ages.
Thanks for the fantastic code!
1 year, 6 months ago
I’m a novice with javascript – I tried to do a test page but was not successful. Do I need to use both the;
Javascript:
var PreSimpleSwapOnload =(window.onload)? window.onload : function(){};
window.onload = function(){PreSimpleSwapOnload(); SimpleSwapSetup();}
and the Html:
If so, were do I put the Javascript code? Do I put it in the head section of my html page? Sorry if I’m naive I’m new to this. I’m using GoLive.
Here’s my test page – http://www.perfect-wedding-day.com/test2.html
It doesn’t seem like my over image is showing.
1 year, 5 months ago
Can any of you guys please check if this JAVA function is OK?
function SimpleSwapSetup()
{
var images = document.getElementsByTagName(”img”);
for (var i=0; ioff.”))
{
// preload image
// comment the next two lines to disable image pre-loading
images[i].oversrcimg = new Image();
images[i].oversrcimg.src = images[i].getAttribute(”src”).replace(”off.”, “on.”);
// set event handlers
images[i].onmouseover = new Function(”SimpleSwap(this, ‘oversrc’);”);
images[i].onmouseout = new Function(”SimpleSwap(this);”);
// save original src
images[i].setAttribute(”origsrc”, images[i].src);
images[i].setAttribute(”oversrc”, images[i].oversrcimg.src);
}
}
}
Im a JAVA moron… I know there is something bad around here:
for (var i=0; ioff.”))
It seems it is in the ioff.” part but Im not sure. Mark, can you help me with this? Im trying to validate a page and this is the last step.
Thanks in advance!
12 months ago
What I would like is a script that “fades” between images at a rate that can be controlled by a parameter. Any suggestions?
Thanx!
hb
10 months, 3 weeks ago
Hello! I am a javascript beginner, I would love to see an alternative version of this script that is W3C standard compatible, I will try and hack it out myself but I’m sure you’d do a much better job and it will make your script a really accessable solution! Please let me know if you update :]
8 months, 2 weeks ago
Thanks for the script… I’ve used it many times over my career.. If anybody is interested in having a link or another element trigger the image swap you can use:
link
8 months, 2 weeks ago
img src="images/home1.jpg" oversrc="images/home1_over.gif" id="home1"
a href="#" onmouseover="SimpleSwap(document.getElementById('home1'),'oversrc');" onmouseout="SimpleSwap(document.getElementById('home1'));"
8 months ago
thanks jehiah for beautiful script.
i’ve used it with @octane’s addition of link code.
maybe some extra automagic code can be inserted into the original functions so that you can change the image with a link in a simple way?
thanks again.
7 months, 3 weeks ago
Hi,
Great code !
It worked perfectly with firefox, but now it doesn’t work with IE7, what could be the problem ?
Thanks !!!
7 months ago
I’m trying to implement exactly what Leah (comment #46) described in her post. Did you have a solution that I can use? I am not a javascripter but I was able to get your sample code to work beautifully on a site I’m designing/coding. Thanks in advance.
5 months, 3 weeks ago
hello there!
thank you very much for the neat code.
however i need to ask you for help
(i read most of the posts above and i think this hasn’t been adressed yet) -
i mouse-over an image, it swaps as it should, i click the image (button), get to another page; so far so good.
now i use the browser’s back button to navigate back -
and the button i’ve clicked before is still in it’s over-state (until i hover over- and off it again). this is on windows xp, FF3.5.
it does, however, work properly in IE7!
any help is much appreciated!
thanks in advance, and have a nice day!
5 months, 1 week ago
if you use jquery, it’s like this:
$(document).ready(function(){
});
the #center img means that every image tag from the element with id center will have rollover effect. the files have to be like this: simple image – image.jpg and onmouseover – image_hover.jpg. you can change everything to suit your needs.
4 months, 3 weeks ago
Slight modification will allow existing mouse events not to be overwritten
//get existing onmouseover and onmouseout events
existingOver = x[i].getAttribute(”onmouseover”);
existingOut = x[i].getAttribute(”onmouseout”);
// set event handlers
x[i].onmouseover = new Function(”SimpleSwap(this,’oversrc’);” + existingOver);
x[i].onmouseout = new Function(”SimpleSwap(this);” + existingOut);
4 months, 3 weeks ago
please note, the above does not work in IE6 for some reason.