Solved

JQuery timed image swap

Posted on 2010-09-12
6
1,163 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

 

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Bootstrap Form 16 54
On Load function does not trigger on iPhone 4 70
modify change color of text 9 50
Jquery syntax 12 29
Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
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 invoke it on a web page. 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.: (CODE)

734 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