Multiple JQuery autocomplete search fields

It’s easy to add multiple search fields using the same autocomplete.js and autocomplete.php, instead of writing one .js and one .php per field.
This is autocomplete.js:

$( ".term" ).live( "focus", function(){
    id=$(this).attr('id');
    $( '#'+id ).autocomplete({
        delay: 300,
        minLength: 2,
        source: "autocomplete.php?ind="+id,
        select: function (event, ui){
            [your code here]
        }
    })
});

Your search fields must be of class “term” (or anything that matches “.term” above in the live() function) and have unique IDs:

<input id="term1" class="term" type="text" />
<input id="term2" class="term" type="text" />

autocomplete.php receives the input field id and performs independent searches.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s