Networking, Programming and Graphics - Tutorials
ONLINEHOWTO.net Tutorials Category

Change IMG SRC picture with Java Script Dynamically

Type: Code Networking, Programming and Graphics - Tutorials
Networking, Programming and Graphics - Tutorials
Level: Beginner Networking, Programming and Graphics - Tutorials 
Networking, Programming and Graphics - Tutorials
Date: 2010-Feb-01
Networking, Programming and Graphics - Tutorials
Visited: 513 times
Networking, Programming and Graphics - Tutorials
Rating: Networking, Programming and Graphics - Tutorials
Networking, Programming and Graphics - Tutorials
Published: Ivory Morhuld

If you have some photo gallery, with many folders of your events and each folder give to user a list with all photos you probably want to display one large photo and the others are small (thumbnails) in some list bar. And when visitors clicks on the thumbnails, on the place of large image to appear large image of the clicked thumbnail.

Of course, you can do this with PHP by sending GET request with image name stored in the url, but this will reload entire webpage, visitor will get sad and more traffic will be generated from your photo gallery (If your photo gallery have many visitors and many photos, probably your internet hosting provider will tell yoo - Go to higher plan, you eat too bandwidth!).
With this tutorial I will show you how to make your photos to be loaded (and displayed) in large variant immediately after when responding thumbnails are clicked. To keep interactivity up we will use Java Script which is executed on visitors browser and for this, no page reloading needed.

First we have to prepare HTML page with some simple thumbnails bar and some place where large images will be loaded. Please note that on the each thumbnail we using handle onclick="showimg(picname);" which call the Java Script function. In this tutorial I will use even a free image hosting service for storing the images. Here is the sample html code:
<table>
    <tr>
        <td colspan="4" height="340" align="center" id="image">Click on the thumbnails bellow</td>
    </tr>
    <tr>
        <td align="center"><img src="http://www.avatarhosting.net/pics/thumb/26402/chrome.jpg" alt="loading..." onclick="showimg('http://www.avatarhosting.net/pics/26402/chrome.jpg');" style="cursor: pointer;"/></td>
        <td align="center"><img src="http://www.avatarhosting.net/pics/thumb/26403/linux.jpg" alt="loading..." onclick="showimg('http://www.avatarhosting.net/pics/26403/linux.jpg');" style="cursor: pointer;"/></td>
        <td align="center"><img src="http://www.avatarhosting.net/pics/thumb/26404/mac.jpg" alt="loading..." onclick="showimg('http://www.avatarhosting.net/pics/26404/mac.jpg');" style="cursor: pointer;"/></td>
        <td align="center"><img src="http://www.avatarhosting.net/pics/thumb/26405/win.jpg" alt="loading..." onclick="showimg('http://www.avatarhosting.net/pics/26405/win.jpg');" style="cursor: pointer;"/></td>
    </tr>
</table>
Now we have to prepare Java Script function which will done the real job. Here is the ready function:
function showimg(img) {
    document.getElementById('image').innerHTML='<img src="'+img+'" alt="Loading...please wait"/>';
}
Now we will embed the Java Script function into photo gallery html file. Here is the complete html source.
<html>
<head>
<script type="text/javascript">
function showimg(img) {
    document.getElementById('image').innerHTML='<img src="'+img+'" alt="Loading...please wait"/>';
}
</script>
</head>
<body>
<table>
    <tr>
        <td colspan="4" height="340" align="center" id="image">Click on the thumbnails bellow</td>
    </tr>
    <tr>
        <td align="center"><img src="http://www.avatarhosting.net/pics/thumb/26402/chrome.jpg" alt="loading..." onclick="showimg('http://www.avatarhosting.net/pics/26402/chrome.jpg');" style="cursor: pointer;"/></td>
        <td align="center"><img src="http://www.avatarhosting.net/pics/thumb/26403/linux.jpg" alt="loading..." onclick="showimg('http://www.avatarhosting.net/pics/26403/linux.jpg');" style="cursor: pointer;"/></td>
        <td align="center"><img src="http://www.avatarhosting.net/pics/thumb/26404/mac.jpg" alt="loading..." onclick="showimg('http://www.avatarhosting.net/pics/26404/mac.jpg');" style="cursor: pointer;"/></td>
        <td align="center"><img src="http://www.avatarhosting.net/pics/thumb/26405/win.jpg" alt="loading..." onclick="showimg('http://www.avatarhosting.net/pics/26405/win.jpg');" style="cursor: pointer;"/></td>
    </tr>
</table>
</body>
</html>
And now lets see the result:
Click on the thumbnails bellow
loading... loading... loading... loading...
Rate this tutorial:                    
Post Comment

Need a specific tutorial? Do not hesitate and submit a request!
Your e-mail: