Basics
Creating a Document
Head and Body Elements
Formatting Text
Creating Lists
Embedding Images
URLs Explained
Linking to Documents
Creating Tables
Forms Form Basics
Input and Textarea
Select Lists
Frames Framesets
The Frame Element
Nested Framesets
Targetting Frames
Inline Frames
Tips and Tricks Meta Tags
Transition Effects
HTML Generators Create a Document
Create a List
Create a Table
HTML
Basics
Implementing CSS
CSS Syntax
Pseudo Classes/Elements
CSS Classes
CSS Properties Font Properties
Color and Background
Text Properties
Border Properties
Margins and Padding
Size and Position
Tips and Tricks Menu Buttons
Special Effects
CSS
Basics
Running a Script
Variables
Expressions and Operators
Objects.Properties.Methods
The Date Object
Strings
Regular Expressions
Defining RegExp Patterns
Branches and Conditions
Loops
Arrays Array Basics
Array Methods
Sorting Arrays

User-Defined Functions
Cookies
Windows
Frames
Tips and Tricks Image Replacement
Using Includes
Form Validation
Debugging
JavaScript
Basics
Creating a Script
Running a Script
Variables
Expressions and Operators
Strings Strings Basics
Strings and Substrings
Replacing Substrings
Regular Expressions
Branches and Conditions
Loops
Arrays Array Basics
Array Functions
Sorting Arrays
User-Defined Functions
Include and Require
Uploading Files
File Functions
Session Variables
Tips and Tricks Page Templates
Form Reply Scripts
Form Validation
JavaScript to PHP
PHP
Basics
Create and Drop
Show and Describe
Insert, Update and Delete
Querying
Join Queries
Functions
Table Locking
PHP/MySQL Functions Accessing a Database
Querying with PHP
Create and Drop with PHP
Insert and Update with PHP
Frequently Used Functions MySQL
Basics
Layout and Navigation
Page Content Style
Web Page Copy
Graphics and Animation
HTML Forms
Accessibility
Legal Requirements
MySQL
JavaScriptBasicsRunning a ScriptVariablesExpressions and OperatorsObjects.Properties.MethodsThe Date ObjectStringsRegular ExpressionsDefining RegExp PatternsBranches and ConditionsLoopsArraysArray MethodsSorting ArraysUser-Defined FunctionsCookiesWindowsFrames

Home > JavaScript Tips and Tricks > Image Replacementprinter version

JavaScript Image Replacement

An image can be made to change when a mouse event is triggered and hence create a slideshow of images or similar.

It relies on using the DOM, a limited version of which was first available in Netscape's version 3 browser (see the tutorial on objects, properties and methods).

When an image is loaded into an HTML page it becomes a property of the document object - but the image is an object in its own right with properties.

The src property of the image object holds the path and file name of the image and JavaScript can be used to write a new path and file name directly to this property - causing it to change to the new image.

If the image isn't already in cache it will download from the server to replace the existing image.

Referring to the image object is made easier if you give it a name within the HTML tag as shown below.

Example Image
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<img src="images/image_1.gif" name="image_1_name">
</body>
</html>


This image can now be referred to with
document.images["image_1_name"].src

Using a Click Event

To change the path and file name within the image object requires an event to occur and a JavaScript link can be used to trigger that event.

A simple JavaScript function will act as the event handler and be passed two arguments. The first is the name of the image to change and the second is the path and file name of the image that will replace the original image.

The Event Handler
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script language="javascript" type="text/javascript">
    function change(image,new_image){
        document.images[image].src=new_image;
    }
</script>
</head>
<body>
<img src="images/image_1.gif" name="image_1_name">
</body>
</html>


Finally all that's needed is a JavaScript link that will invoke the event handler when clicked.

The Complete code
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script language="javascript" type="text/javascript">
    function change(image,new_image){
        document.images[image].src=new_image;
    }
</script>
</head>
<body>
<img src="images/image_1.gif" name="image_1_name">
<br>
<a href="javascript:change('image_1_name','images/image_2.gif')">Change Image</a>
</body>
</html>


Using an onmouseover Event

Alternatively if you wish to use the image as a link and have it change onmouseover then the code is straightforward.

The same event handler may be used with an HTML link that includes an onmouseover event with arguments to change to the new image and an onmouseout event with arguments to change back to the original image.

With an onmouseover Event
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script language="javascript" type="text/javascript">
    function change(image,new_image){
        document.images[image].src=new_image;
    }
</script>
</head>
<body>
<a href="next_page.htm" onmouseover="change ('image_1_name','images/image_2.gif')" onmouseout="change ('image_1_name','images/image_1.gif')"><img src="images/image_1.gif" name="image_1_name" border="0"></a>
</body>
</html>





Privacy | Terms | Contact | Links | Sitemap | RSS Feeds RSS and JavaScript Feeds
©2010 www.webdesignworkmate.co.uk all rights reserved 
Design and Production by smallbizonline website design © 2000-2010
Valid HTML 4.01! Level Double-A conformance icon, W3C-WAI Web Content Accessibility Guidelines 1.0Valid CSS!
Tips and TricksJavaScript image replacementImage ReplacementUsing IncludesForm ValidationDebugging
Got any JavaScript Tips?
Send me your tip and if it's suitable I'll put it on the site, credit it to you and add a link back to your site.
Recommended Reading
Javascript the definitive guide

Javascript and DHTML cookbook

Pro javascript techniques

simply javascript