Solved

JQuery timed image swap

Posted on 2010-09-12
6
1,161 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

DevOps Toolchain Recommendations

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

Question has a verified solution.

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

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
This article discusses how to create an extensible mechanism for linked drop downs.
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…

810 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