1

I am having 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>
4
  • 2
    Is this a learning exercise, or for an actual website? If the latter, I would recommend you give jQuery a shot. That 16 or so lines of script would likely reduce to about 2 or 3. Commented Nov 30, 2011 at 23:18
  • 1
    @AdamRackis, i'm pretty sure i'd use one line of jQuery for what he's doing...i feel so spoiled. Commented Nov 30, 2011 at 23:36
  • have never used jQuery, can you show me some code that would solve problem? thanks :) Commented Dec 1, 2011 at 7:21
  • @zzzzBov, do you mind writing that one line here for me? Commented Dec 1, 2011 at 10:08

1 Answer 1

1

I'd say you don't need the lines that are resetting the onmouseover events.

There's no need to rewrite the onmouseover events - all you want to change is the img src attribute.

As Adam mentions, there's more modern ways to do this using jQuery - look at:

http://code.google.com/p/jquery-swapimage/

For example.

Sign up to request clarification or add additional context in comments.

1 Comment

If i don't reset the onmouseover events then the image that is selected will do the exact same thing like other images.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.