Javascript: onmouseover function

I have a problem with changing onmouseover and onmouseout attributes on dynamic pictures. The way i want it to work is whenever i put my mouse over images the images must change and when i take my mouse away it must be changed to the original picture. and whenever i select any image, that image must be changed to the image which was displayed while moving the mouse across the image. and when i select any other image the same process must take place but the previous image that was changed must be changed back to the original picture.

I have accomplished all of the above but my problem is when i select multiple pictures and put my mouse over images that were previously selected, those images do not change (onmouseover attribute does not work on them anymore).

<script language="javascript">
    function changeleft(loca){
        var od=''
        var imgs = document.getElementById("leftsec").getElementsByTagName("img"); 
        for (var i = 0, l = imgs.length; i < l; i++) {  

                od = imgs[i].id;
                this.onmouseover = function (){this.src="images/"+od+"_over.gif";};
                    this.onmouseout = function (){this.src="images/"+od+".gif";};


<div class="leftsec" id="leftsec" >
    <img id='wits' class="wits1"  src="images/wits.gif" onmouseover="this.src='images/wits_over.gif'" onmouseout="this.src='images/wits.gif'" onclick="changeleft(" /><br />

    <img id='city' class="city1" src="images/city.gif" onmouseover="this.src='images/city_over.gif'" onmouseout="this.src='images/city.gif'" onclick="changeleft(" /><br />

    <img id='organise' class="city1" src="images/organise.gif" onmouseover="this.src='images/organise_over.gif'" onmouseout="this.src='images/organise.gif'" onclick="changeleft(" /><br />

    <img id='people' class="city1" src="images/people.gif" onmouseover="this.src='images/people_over.gif'" onmouseout="this.src='images/people.gif'" onclick="changeleft(" /><br />


I would recommend to use an Ajax library (jQuery, YUI, dojo, ExtJS, ...). In jQuery I would do something like:

Edit: Extended the example with .click() ability.

var ignoreAttrName = 'data-ignore';

var imgTags = jQuery('#leftsec img'); // Select all img tags from the div with id 'leftsec'

.attr(ignoreAttrName , 'false') // Supplying an ignore attribute to the img tag
.on('click', function () {
    jQuery(imgTags).attr(ignoreAttrName, 'false'); // Resetting the data tag
    jQuery(this).attr(ignoreAttrName, 'true'); // only the current will be ignored
    // Do whatever you want on click ...
.on('mouseover', function () {
    // This will be called with the img dom node as the context
    var me = jQuery(this);

    if (me.attr(ignoreAttrName) === 'false') {

        me.attr('src', me.attr('id') + '.gif');
.on('mouseout', function () {
    // This will be called when leaving the img node
    var me = jQuery(this);

    if (me.attr(ignoreAttrName) === 'false') {
        me.attr('src', me.attr('id') + '-over.gif');

With a library it's cleaner and more scalable I think and the chance that it's working in other browsers is increased too :).

Hope this helps you out!

Need Your Help

JAX-RS support for file upload

rest jersey resteasy

I researched ways to write a file upload application. The examples either make use of Jersey or RESTEasy specific annotations to implement the application.

How to 'merge' a XAML file and its code-behind with Visual Studio

wpf visual-studio xaml code-behind

I have a template defined in a XAML file named 'MyTemplate.xaml'. This template is using a code-behind file named 'MyTemplate.cs'.