I have two pictures. one arrow_right.png and arrow_left.png. I want to display Only one picture. If Clicked then Toggles to another picture. Can you please help me how to it with jQuery toggle() function. Because it is hiding my picture.
            Asked
            
        
        
            Active
            
        
            Viewed 483 times
        
    0
            
            
         
    
    
        Jasper
        
- 75,717
- 14
- 151
- 146
 
    
    
        Nabeel Arshad
        
- 45
- 1
- 7
- 
                    possible duplicate of [Changing the image source using jQuery](http://stackoverflow.com/questions/554273/changing-the-image-source-using-jquery) – brendan Feb 08 '12 at 06:18
- 
                    Can you post the code you have tried? – Jasper Feb 08 '12 at 06:18
1 Answers
3
            $('img').toggle(
    function () {
        $(this).attr('src', 'arrow_left.png');
    },
    function () {
        $(this).attr('src', 'arrow_right.png');
    }
);
Docs for .toggle(): http://api.jquery.com/toggle
Here is a demo: http://jsfiddle.net/dQZfh/
Update
You can also animate the change between each image by using .animate() or some of the pre-build animations:
$('img').toggle(
    function () {
        $(this).stop().fadeTo(250, 0, function () {
            $(this).attr('src', 'http://chachatelier.fr/programmation/images/mozodojo-mosaic-image.jpg').fadeTo(250, 1);
        });
    },
    function () {
        $(this).stop().fadeTo(250, 0, function () {
            $(this).attr('src', 'http://www.prelovac.com/vladimir/wp-content/uploads/2008/03/example.jpg').fadeTo(250, 1)
        });
    }
);
Notice that I used the callback function for fadeTo to change the source only after the image has faded-out completely.
Here is a demo (fade): http://jsfiddle.net/dQZfh/1/
Here is a demo (slide): http://jsfiddle.net/dQZfh/2/
