Search code examples
javascripthtmlonmouseoveronmouseout

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;

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

        }
    }
</script>

<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(this.id)" /><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(this.id)" /><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(this.id)" /><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(this.id)" /><br />
</div>

Solution

  • 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'
    
    jQuery(imgTags)
    .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!