Solved

JQuery timed image swap

Posted on 2010-09-12
6
1,159 Views
Last Modified: 2012-05-10
Hello experts!

Can someone tell me how I can swap an image with another for a set amount of time when a button is clicked using JQuery. I've managed to show an image for a set amount of time but swapping has deluded me? Thanks
<script type="text/javascript">

$(document).ready(function(){

	$('#click_to_load').click(function(){

		$('#loader').show(0).delay(500).hide(0);

		return false;

	});

});

</script>

Open in new window

0
Comment
Question by:allanch08
  • 3
  • 3
6 Comments
 
LVL 58

Expert Comment

by:amit_g
ID: 33657497
It would be easier if corresponding HTML was also posted. What is #loader? A way would be to have two of those and show one and hide another after the set delay.

<script type="text/javascript">
$(document).ready(function(){
      $('#click_to_load').click(function(){
            $('#loader1').hide(0).delay(500).show(0);
            $('#loader2').show(0).delay(500).hide(0);
            return false;
      });
});
</script>
0
 

Author Comment

by:allanch08
ID: 33657520
Hello,

#loader is the id of the div containing the image. so basically clicking the button with id of click_to_load shows the div with id ="loader".
<div id="loader">

    <img src="images/loading.gif" alt="Loading...">

</div><!-- end div -->

Open in new window

0
 
LVL 58

Expert Comment

by:amit_g
ID: 33657619
<div id="loader">
    <img id="img1" src="http://www.google.com/images/firefox/firefox1.png" alt="Loading...">
    <img id="img2" src="http://www.google.com/images/firefox/sync-addon.png" alt="Loading..." style="display:none">
</div><!-- end div -->


<script type="text/javascript">
$(document).ready(function(){
      $('#click_to_load').click(function(){
            $('#loader')..show(0);
            setTimeout("$('#loader img').toggle();", 3000);
            return false;
      });
});
</script>
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:allanch08
ID: 33657821
thanks, can you tell me how to get it to revert back to the original image again after the second image is shown?
0
 
LVL 58

Accepted Solution

by:
amit_g earned 500 total points
ID: 33658807
Toggle again...

<script type="text/javascript">
$(document).ready(function(){
      $('#click_to_load').click(function(){
            $('#loader')..show(0);
            setTimeout("$('#loader img').toggle();", 3000);
            setTimeout("$('#loader img').toggle();", 6000);
            return false;
      });
});
</script>
0
 

Author Comment

by:allanch08
ID: 33659834
cool, thanks for your help amit_g! much appreciated!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now