Mouseover image change through javascript - change images on web page on mouseover
mouseover image change through javascript, change images on web page on mouseover
JavaScript - Image Change Using FunctionsGo to JavaScript - Image Change Using FunctionsJavaScript tutorialGo to JavaScript tutorialWeb development tutorialsGo to web development tutorialsHomepage

Main navigation links of JavaScript Tutorial section

JavaScript - Image Change Using Functions

The main use of function is to prevent us from writing the same code again and again because we can call the same function with a different set of parameters.

In the image roll-over script of the previous session, the code for changing the image was placed inside the event handlers. This is fine if there are one or two images. In case of several images, it is advisable to pack the code in a function that can be called repeatedly by event handlers using different parameters.

There are two important things in an image roll-over code. One is the image name and the other is the image source. Thus, we have to construct a function that accepts these two arguments and changes the image source.

function roll_over(img_name, img_src)
   {
   document[img_name].src = img_src;
   }

We can now call this function from our even handlers and pass it the two arguments.

<A HREF="some.html" onmouseover="roll_over('but1', 'icon2.gif')"
onmouseout="roll_over('but1', 'icon1.gif')">
<IMG SRC="icon1.gif" WIDTH="100" HEIGHT="50"
NAME="but1" BORDER="0">
</A>
Button image changes on mouse over


Assignments
  1. You've seen how we can change an image on mouseover. How do you think, two images can be changed with a single mouse over?
   Click here for possible answers


Online Workspace

Back Next


AddThis Social Bookmark Button
Page contents: Mouseover image change through javascript - change images on web page on mouseover

Page URL: http://www.webdevelopersnotes.com/tutorials/javascript/ javascript_image_change_functions.php3



Join Mailing List


Feedback/Questions




50+ web hosting FAQs

Search engine for your website

Free software from Google - The Google Pack Collection

Create your own search engine
Search WebDevelopersNotes.com