Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Mouse over ploblem

    I wrote this code for 1 image, and it is working perfectly fine:

    Code:
    function swapImage()
    
    				{
    					document.getElementById("linkedin").src = "images/glinkedin.png";
    				}
    
    function restoreImage()
    
    				{
    					document.getElementById("linkedin").src = "images/linkedin.png";
    				}
    the problem starts when I want to use this code for 4 images, can anyone correct it please:

    Code:
    function swapImage()
    
    				{
    					document.getElementById("linkedin").src = "images/glinkedin.png";
    				}
    
    
    				{
    					document.getElementById("facebook").src = "images/gfacebook.png";
    				}
    
    
    				{
    					document.getElementById("google").src = "images/ggoogle.png";
    				}
    
    
    				{
    					document.getElementById("twitter").src = "images/gtwitter.png";
    				}
    
    function restoreImage()
    
    				{
    					document.getElementById("linkedin").src = "images/linkedin.png";
    				}
    
    				{
    					document.getElementById("facebook").src = "images/facebook.png";
    				}
    
    				{
    					document.getElementById("google").src = "images/google.png";
    				}
    
    				{
    					document.getElementById("twitter").src = "images/twitter.png";
    				}

  • #2
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    You have so much brackets going on there. This should work:
    Code:
    function swapImage() {
    	document.getElementById("linkedin").src = "images/glinkedin.png";
    	document.getElementById("facebook").src = "images/gfacebook.png";
    	document.getElementById("google").src = "images/ggoogle.png";
    	document.getElementById("twitter").src = "images/gtwitter.png";
    }
    
    function restoreImage() {
    	document.getElementById("linkedin").src = "images/linkedin.png";
    	document.getElementById("facebook").src = "images/facebook.png";
    	document.getElementById("google").src = "images/google.png";
    	document.getElementById("twitter").src = "images/twitter.png";
    }
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #3
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Alternatively, you might want to use this instead:
    Code:
    function swapImage(imgBase, ids_imgs) {
    	for(var i in ids_imgs)
    		document.getElementById(i).src = imgBase.imgBase+ids_imgs[i];
    }
    
    function restoreImage(imgBase, ids_imgs) {
    	for(var i in ids_imgs)
    		document.getElementById(i).src = imgBase.imgBase+ids_imgs[i];
    }
    For Usage:
    Code:
    /***
    // swapImage(@argument1, @argument2)
    // @argument1 - base directory of the images
    // @argument2 - pass the element ID to change and the image's source
    ***/
    swapImage({'imgBase':'images/'},{'linkedin':'glinkedin.png',
    			'facebook':'gfacebook.png',
    			'google':'ggoogle.png',
    			'twitter':'gtwitter.png'
    		});
    
    /***
    // restoreImage(@argument1, @argument2)
    // @argument1 - base directory of the images
    // @argument2 - pass the element ID to change and the image's source
    ***/
    restoreImage({'imgBase':'images/'},{'linkedin':'linkedin.png',
    			'facebook':'facebook.png',
    			'google':'google.png',
    			'twitter':'twitter.png'
    		});

    Hope that helps.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #4
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thx, I did it before without so many brackets but the problem is that I want to use every image separately not all of them at the same time. In other words when I hover over one image I want it to change for another one; and other 3 will stay the same. I only want to change the image I hover.

  • #5
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Use this script instead:
    Code:
    function swapImage(el,img_src) {
    	restoreImage(); // restore all images first
    	document.getElementById(el).src = img_src;
    }
    
    function restoreImage() {
    	var orig = {
    		'baseDir':'images/', // base dir of the images
    		'defaultImg': [ // set default images in format [ID , IMAGE_SOURCE]
    				['linkedin','linkedin.png'],['facebook','facebook.png'],
    				['google','google.png'],['twitter','twitter.png']
    			]
    		};
    			
    	for(var i=0,o=orig.defaultImg;i<o.length;i++) 
    		document.getElementById(o[i][0]).src = orig.baseDir + o[i][1];
    }
    ...and when you call swapImage() function, use it like:
    <a href="#" onclick="swapImage('google','images/ggoogle.png');">


    Hope that helps.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #6
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thx, but this is not what I wanted I want to work the same like menu on websites when you place your mouse over menu button it change color.
    Besides I place my function call here:

    Code:
    <img border="0" src="images/google.png" alt="google" id ="google" onmouseover="swapImage()" onmouseout="restoreImage()" width="24" height="24" /></a>

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,184
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    Do it the *EASY* way. Stop working so hard!
    Code:
    <img border="0" src="images/google.png" alt="google" 
         onmouseover="this.src='images/ggoogle.png';"
         onmouseout="this.src='images/google.png';" 
         width="24" height="24" />
    Now you don't even need the id on your image.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,184
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    If you really think you need to use a function:
    Code:
    <script>
    function iover( image ) {
        image.src = image.src.replace( "/", "/g" );
    }
    function iout(image) {
        image.src = image.src.replace( "/g", "/" );
    }
    </script>
    ...
    <img src="images/google.png" onmouseover="iover(this)" onmousout="iout(this)" .../>
    But I don't see much advantage in that.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thx, onmouseover works great


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •