Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As you're using the jQuery UI autocompleter, you can get the element from the <strong><em>undocumented</em></strong> <code>element</code> property on the autocompleter instance:</p> <pre><code>source: function(request, response) { if (this.element &amp;&amp; this.element[0]) { display("source triggered for #" + this.element[0].id); } } </code></pre> <p><a href="http://jsbin.com/ubunip" rel="nofollow">Live example</a></p> <p><code>this.element</code> is a jQuery wrapper for the element the autocompleter is attached to, so <code>this.element[0]</code> is the raw DOM element.</p> <p>But using undocumented information is always risky, it can change or go away between dot releases. It would be more reliable to use a closure as suggested by SadullahCeran. I'd do it slightly differently:</p> <pre><code>$("#input1, #input2").each(function(){ var $element = $(this); // `element` is a jQuery wrapper around the element $(element).autocomplete({ source:function( request, response ) { // get the options relevant to `element` }); }); </code></pre> <p>...just because then you're not relying on using an <code>id</code> value. But that's a small point. And yes, the above does end up creating two function objects, but that's not such a bad thing. If there's a lot of code involved and you're worried about having two copies of it in memory (which is almost certainly not an issue), just have the function call into another function:</p> <pre><code>$("#input1, #input2").each(function(){ var $element = $(this); // `element` is a jQuery wrapper around the element $(element).autocomplete({ source:function( request, response ) { return getOptionsFor(element, request, response); }); }); function getOptionsFor(element, request, response) { // get the options for `element` } </code></pre>
 

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