Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery change and click event timing
    primarykey
    data
    text
    <p>I seem to have a timing problem with my jQuery change and click events. In my site I have it with an Ajax post instead of an alert, but it's easier to reproduce it using the alert. I have a simple HTML page using jQuery Version 1.7.1.</p> <p>The scenario is editing the textbox and then directly clicking the button. The change event is always fired but if it contains a alert the click event is never called. If I cheat and put a timeout around the alert (you might have to play around a bit with the values there) the click event is called normally.</p> <p>/<strong>** EDIT **</strong>/ If you set a breakpoint somewhere in the change event and wait for a few seconds the click event will aslo fail to fire.</p> <pre><code>&lt;script type="text/javascript"&gt; $(document).ready(function () { $("input[type=text]").change(function () { //alert('changed'); $("#text").text('changed'); setTimeout(function () { $("#text").text('changed timeout'); alert('changed'); }, 100); }); $("input[type=submit]").click(function () { alert('clicked'); $("#text").text('clicked'); }); }); &lt;/script&gt; &lt;div id="main"&gt; &lt;div class="container"&gt; &lt;input type="text" /&gt; &lt;input type="submit" value="click me" /&gt; &lt;div id="text" &gt;bla&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>My Ajax call that has the save behavoir is the following:</p> <pre><code>$.ajax({ type: 'POST', url: postUrl, data: form.serialize(), beforeSend: function () { // display overlay for work in progress }, success: function (result) { updateTableSuccess(); }, complete: function () { // hide overlay for work in progress }, error: function (jqXHR) { if (jqXHR.responseText.length &gt; 0) { showErrorMessage(jqXHR.responseText); } } }); </code></pre> <p>The overlay is simply appending 2 divs to the body Tag.</p> <p>/<strong>** Edit **</strong>/ It seems that the overlay is the real problem and causes the same behavoir as the alert and stops the click event from being fired.</p> <pre><code>showWorkInProgress = function (message, elementId) { if ($("#overlay").length == 0) { $("&lt;div id='overlay' class='ui-widget-overlay'&gt;&lt;/div&gt;").appendTo("body"); } if ($("#workInProgress").length == 0) { $("&lt;div id='workInProgress'&gt;&lt;/div&gt;").appendTo("body"); } $("#workInProgress").html(message); if (elementId != null) { $("#workInProgress").center(elementId); } else { $("#workInProgress").center(); } $("#overlay").show(); $("#workInProgress").show(); }; </code></pre> <p>Can anyone explain why jQuery behaves like this and how I could do this nicer without the setTimeout hack?</p> <p>Thanks for any help =)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload