jQuery - Changing image src with toggle function
 
Rating: 31 user(s) have rated this article 4.4 out of 5 stars
Posted by: jstrosch01, on 6/26/2008, in category "jQuery"
Views: this article has been read 24004 times


Delicious Bookmark this on Delicious

Using jQuery to do more than just toggle the display of an element is fairly straight-forward.  All of the code here is depenedent on the core jQuery library, which can be found at jQuery.com. Make sure you include this in your head tags like so:

< script type="text/javascript" src="jquery.js" >< /script>

Here is some sample html to help illustrate this example:

< div id="toggler">

      < img src="arrowDown.gif" id="imgArrows" / > Click Me

< /div>

< div id="box" style="width:200px;height:200px;display:none;background-color:#CCC;">

     Some Content...

< /div>

Now all you have to do is write a few lines of jQuery:

< script type="text/javascript">

     $(document).ready(function(){

          $('#toggler').toggle(

               function(){ // you can add as much here as you'd like

                    $('#box').show('slow');

                    $('#imgArrow').attr('src','path/to/image');

               }, function() { // same here

                    $('#box').hide('slow');

                    $('#imgArrow').attr('src','path/to/different_image');

               });

     });

< /script>

So, combine this and you get the following:

< html xmlns="http://www.w3.org/1999/xhtml">

< head>

     < script type="text/javascript" src="jquery.js" >< /script>

     < script type="text/javascript">

          $(document).ready(function(){

               $('#toggler').toggle(

                    function(){ // you can add as much here as you'd like

                         $('#box').show('slow');

                         $('#imgArrow').attr('src','path/to/image');

                    }, function() { // same here

                         $('#box').hide('slow');

                         $('#imgArrow').attr('src','path/to/different_image');

                    });

          });

     < /script>

< /head>

< body>

     < div id="toggler">

          < img src="arrowDown.gif" id="imgArrows" /> &nbsp; Click Me

     < /div>

     < div id="box" style="width:200px;height:200px;display:none;background-color:#CCC;">

          Some Content...

     < /div>

< /body>

< /html>

 Live Example:

 Click here to toggle

 

 

Share and Bookmark

LinkedIn Google Del.icio.us Digg Reddit Stumble Upon

DISCLAIMER: This information is provided "as is". The author, publishers and marketers of this information disclaim any loss or liability, either directly or indirectly as a consequence of applying the information presented herein, or in regard to the use and application of said information. No guarantee is given, either expressed or implied, in regard to the merchantability, accuracy, or acceptability of the information.


How would you rate this article?

User Feedback
Comment posted by Marty on Tuesday, September 23, 2008 1:36 PM
Stumbled...

Nice example..
Comment posted by MATT on Friday, December 12, 2008 12:11 PM
How would I change this to work with multiple toggles, each showing and hiding it's own DIV?
Comment posted by Josh on Tuesday, December 16, 2008 7:25 AM
You would need to define the jquery for each div you would want to have toggle capability. So if you had:

<div id="one"><a img="/path/to/image" id="imgOne"/></div><br />
<div id="two"><a img="/path/to/image" id="imgTwo"/></div>


<br /><br /><div id="contentOne"></div>
<br /><div id="contentTwo"></div>

<br /><br />You would need jquery like this:<br /><br />
$(document).ready(function(){<br />
   &nbsp;&nbsp;function(){ <br />
  $('#contentOne').show('slow');<br />
      $('#imgOne').attr('src','path/to/image');<br />
    }, function() { // same here<br />
      $('#contentOne').hide('slow');<br />
      $('#imgOne').attr('src','path/to/different');<br />
    });<br />
<br />
    function(){ <br />
  $('#contentTwo').show('slow');<br />
  $('#imgTwo').attr('src','path/to/image');<br />
     }, function() { // same here<br />
        $('#contentTwo').hide('slow');<br />
        $('#imgTwo').attr('src','path/to/different');<br />
     });<br />
});<br /><br />

<br /><br />You can change up what element you want to toggle the content div's. Hope this helps.
Comment posted by Josh on Tuesday, December 16, 2008 7:26 AM
Sorry for the inconvenience, I'll clean up the ascii tonight.
Comment posted by Web%20World%20Guru on Saturday, January 24, 2009 2:31 AM
Hi,
Thanks I was searching for this code. Your jquery script is amazing, But it didnt work for me.
After making few changes in you code
ex. $('#imgArrow').attr('src','path/to/image');
changed to
$('#imgArrows').attr("src","path/to/image");

correct syntax is double quotes and not single quotes.

visit http://www.webworldguru.com for more examples on jquery.
Comment posted by mike on Tuesday, February 03, 2009 11:54 AM
how would i go about making the default option be that the box starts off expanded?
Comment posted by Josh on Thursday, February 05, 2009 7:28 AM
In the div with the id of 'box' I set the display property to none, which doesn't display the div.  If you leave out this property then the div would be visible when the page loads.
Comment posted by Jerry on Monday, March 02, 2009 6:28 PM
Dont know if anyone of you guy noticed that on IE6 the .attr("src") returns a full path, wich means "http://www.midwestdotnet.com/images/image1.png" pay attention to that or you will have problems like i do, guess its getter toggle classes
Comment posted by Janckos on Saturday, April 25, 2009 2:08 PM
gracias por el tutorial.
Comment posted by Peter%20from%20Lebanon on Tuesday, September 15, 2009 2:26 PM
very good script.
I used it in one of the webpages I design.
thanks for sharing.
Comment posted by cable on Wednesday, February 24, 2010 4:49 AM
I like this script but its not perfect.
Comment posted by Jody on Monday, March 01, 2010 1:43 PM
Very helpful and simple.  Thank you.
Comment posted by Aritra on Tuesday, July 27, 2010 4:16 AM
Cool Stuff man!

Post your comment
Name:
Comment:
Insert Cancel
Questions...
Have you upgraded to Windows 7?


Archived Polls