Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind jQuery UI autocomplete using .live()

I've searched everywhere, but I can't seem to find any help...

I have some textboxes that are created dynamically via JS, so I need to bind all of their classes to an autocomplete. As a result, I need to use the new .live() option.

As an example, to bind all items with a class of .foo now and future created:

$('.foo').live('click', function(){   alert('clicked'); }); 

It takes (and behaves) the same as .bind(). However, I want to bind an autocomplete...

This doesn't work:

$('.foo').live('autocomplete', function(event, ui){   source: 'url.php' // (surpressed other arguments) }); 

How can I use .live() to bind autocomplete?

UPDATE

Figured it out with Framer:

$(function(){   $('.search').live('keyup.autocomplete', function(){     $(this).autocomplete({       source : 'url.php'     });   }); }); 
like image 998
sethvargo Avatar asked Dec 29 '10 03:12

sethvargo


People also ask

How does autocomplete work in jQuery?

In the process of searching a specific value, the jQuery UI autocomplete selection feature provides the user with some string suggestions for the input area which effectively saves time. The autocomplete select action is triggered when the user selects one of the options from the pre-populated list.

How can create autocomplete search box in jQuery?

For this, we are going to use the jQuery inbuilt function called autocomplete. We will do that in two different stages. Create a basic HTML file and add an input bar to it. Implement the autocomplete functionality.

What is autocomplete example?

A user types into an input, and the autocomplete “completes” their thought by providing full terms or results: this is the very base of an autocomplete experience. For example, try typing the letter “m” in the search box below. You can select suggestions like “macbook” and “mobile”.


1 Answers

jQuery UI autocomplete function automatically adds the class "ui-autocomplete-input" to the element. I'd recommend live binding the element on focus without the "ui-autocomplete-input" class to prevent re-binding on every keydown event within that element.

$(".foo:not(.ui-autocomplete-input)").live("focus", function (event) {     $(this).autocomplete(options); }); 

Edit

My answer is now out of date since jQuery 1.7, see Nathan Strutz's comment for use with the new .on() syntax.

like image 190
Dan Avatar answered Sep 18 '22 14:09

Dan