Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I add a `loading...` icon to jQuery UI's autocomplete widget
    primarykey
    data
    text
    <p>I'm trying to modify <a href="http://jqueryui.com/autocomplete/" rel="nofollow">jQuery's autocomplete widget</a> to have the input's background color change while the script is searching for suggestions. Here is my attempt (or have a look at <a href="http://jsfiddle.net/Shawn/hbP7N/" rel="nofollow">my attempt on jsfiddle</a>):</p> <p><strong>html</strong></p> <pre><code>&lt;label for="numbers"&gt;Numbers: &lt;/label&gt; &lt;input id="numbers" /&gt; </code></pre> <p><strong>javascript</strong></p> <pre><code>var numbers = [], nb = 10000, i; for (i = 0; i &lt; nb; i += 1) { numbers.push(i.toString()); } function color (color) { $('#numbers').css({ background: color }); } $("#numbers").autocomplete({ source: numbers, search: function (event, ui) { console.log('search event fired'); color("red"); }, open: function (event, ui) { console.log('open event fired'); color("green"); }, close: function (event, ui) { console.log('close event fired'); color("white"); } }); </code></pre> <p>As you can see, I'm logging the <code>search</code>, <code>open</code> and <code>close</code> events so I can see when they are fired.</p> <p>As expected, typing in <code>5</code> (an existing value) fires the <code>search</code> event and logs the corresponding message, and, a few seconds later, fires the <code>open</code> event and logs its corresponding message. Since I put 10000 entries in there, there is a noticeable delay between the <code>search</code> event and the <code>open</code> event (something like 1 second).</p> <p>What baffles me is that when the log message associated to the <code>search</code> event appears, it is not followed by the background color changing to red as should be expected. Instead, it remains white until the <code>open</code> event occurs, then becomes green (as expected after the open event). If, however, I type in <code>a</code> (a non-existing value), the background color goes red without problem (notice the open event never occurs in this case because no corresponding value is found). What's happening here?</p> <p>For the curious, I'm ultimately trying to have a little <code>loading</code> icon appear while the search is underway (a feature which I am surprised isn't shipped with the widget out of the box). The background color change is a first step in that direction.</p> <p><strong>UPDATE</strong></p> <p>I made <a href="http://jsfiddle.net/Shawn/TMMHE/1/" rel="nofollow">another attempt</a> which shows that the instruction following <code>console.log</code> is indeed called, but the visual effects appear only much later. Here is the code I'm using: </p> <pre><code>$("#numbers").autocomplete({ source: numbers, search: function (event, ui) { console.log('search event fired'); $('#numbers').css({ marginTop: "5em" }); console.log('search callback done'); } }); </code></pre> <p>If you try it out, you'll see that both messages are logged before the field is displaced by the addition of a top margin. Somehow, the margin is added at the right time, but the page is not being redrawn at the right time...</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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