Solved

jquery event onclick work one time

Posted on 2013-06-27
9
584 Views
Last Modified: 2013-06-27
Hello,
I have a problem. I want to catch each event onclick on a button but it only work one time ...

here the code  (html/php/jquery):
<form align="center" action="InfoCommande.php" method="POST" name="InfoCommande" id="InfoCommande">
	<input type="hidden" name="pDevis" value="<? echo $pDevis; ?>">
	<TEXTAREA  name="InfoCommande" value="" cols="50" rows="2" wrap="physical"><? echo $Int[0];?></TEXTAREA>
	<div id="r" style="display: inline;" > <input type="button" id="action" value="Ok" class='newSmallbouton' >  </div>
</form>


 <script>
      $(function() {
        $('#action').on('click', function(event) {
        	$('#r').html('<img src="img/ajax-loader.gif">');
       		setTimeout(function() {
  				$('#r').html('<input type="button" id="action" value="Ok" class="newSmallbouton"  >');}, 1000);
      });
      });
</script>

Open in new window

0
Comment
Question by:bruno_boccara
  • 4
  • 3
  • 2
9 Comments
 
LVL 42

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 39280798
You are adding the click event to your #action button when the page loads and then you destroy it and re-create it so the click event is no longer bound to it. You need to delegate the click event to something your don't destroy, like the InfoCommande form:

<script>
    $(function() {
        $("#InfoCommande").on("click", "#action", function(event){
        ...

Open in new window

0
 

Author Closing Comment

by:bruno_boccara
ID: 39280811
Many thanks for your quick answer .....
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 39280821
You need to re-attach the event handler to the removed button.

Instead simply do this:

DEMO

<form align="center" action="InfoCommande.php" method="POST" name="InfoCommande" id="InfoCommande">
	<input type="hidden" name="pDevis" value="<? echo $pDevis; ?>">
	<TEXTAREA  name="InfoCommande" value="" cols="50" rows="2" wrap="physical">bla</TEXTAREA>
	<div id="r" style="display: inline;" > <input type="button" id="action" value="Ok" class='newSmallbouton' > 
        <img src="http://www.gseforsale.aero/images/ajax-loader.gif" id="throbber" style="display:none" />
    </div>
</form>

Open in new window

$(function() {
  $('#action').on('click', function(event) {
    $('#action').hide();
    $("#throbber").show();  
    setTimeout(function() {
      $("#throbber").hide();  
      $('#action').show();},
    1000);
  });
});

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 39280824
Hmm very quick reply/accept...
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 42

Expert Comment

by:Chris Stanyon
ID: 39280835
@mplungjan - If it works, it works... ;)
0
 

Author Comment

by:bruno_boccara
ID: 39280848
the first solution it works perfectly ... (:-)

So I gave him the points ....

Many thanks to you too.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 39280945
The reason I was grumbling was that I did not post the initial solution because I did not like it since it is needlessly inserting and removing elements from the page so I went to write a better one but was too late.
0
 
LVL 42

Expert Comment

by:Chris Stanyon
ID: 39280953
I wouldn't say needlessly - it inserts a 'loading' image when something is loading and removes it once it's done - seems logical to me.

Always having a 'loading' image in the DOM seems a little needless - think semantic markup! Makes much more sense to bring in elements when you need them and remove when you don't.

That's the thing - there's always more than one way to do it :)
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 39281117
Hmm. that does make sense. Oh well I should have posted first and fiddled later ;)
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

747 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

18 Experts available now in Live!

Get 1:1 Help Now