views:

167

answers:

2

I am creating autocomplete functionality on an input tag using following code.

$('.query').autocomplete({
    serviceUrl:'http://localhost/main/finder.php',
    minChars:2,
    delimiter: /(,|;)\s*/, // regex or character
    maxHeight:400,
    width:400,
    zIndex: 9999,
    deferRequestBy: 0, //miliseconds
    onSelect: function(value, data){
    }
    });

Now the problem is my input element is added dynamically so for first input tag autocomplete is working but when i add one more input tag then it fails for the second one.

so i need some facility that live() provide in jquery ...

please do post the solution

+1  A: 

You're looking for the livequery plugin:

$('.query').livequery(function() {
    $(this).autocomplete({
        serviceUrl:'http://localhost/main/finder.php',
        minChars:2,
        delimiter: /(,|;)\s*/, // regex or character
        maxHeight:400,
        width:400,
        zIndex: 9999,
        deferRequestBy: 0, //miliseconds
        onSelect: function(value, data){
        }
    });
});

This will run the function whenever new elements matching the selector are added.

SLaks
@hunt note that you should make sure that your dynamic page updates are going through the "standard" jQuery APIs.
Pointy
well i am using following plugin for jquery http://www.devbridge.com/projects/autocomplete/jquery/
hunt
How are you changing the content?
SLaks
can plz tell me what sort of content r u talking abt ?
hunt
How are you adding the input?
SLaks
when i click on a particular div with id "ft" at that time i add input element dynamically like as follows$("#ft").live('dblclick',function(){ // add input element // attach autocomplete });but by attaching autocomplete in a above section is creating a new object of autocomplete everytime ...
hunt
You need to create a new autocomplete object every time. There is no other way to do it.
SLaks
but if i do that thing then eventually there are number of autocomplete tag on my page and at the end they faild to behave in a proper manner ...
hunt
+1  A: 

This should help --> http://stackoverflow.com/questions/1492198/jquery-auto-complete-for-dynamically-generated-textboxes

ryanulit
Well i have added following line when dynamically input tag with class 'query' is created $(this).children('.query').autocomplete({....});It works fine as i saw the approach in above link but does this increase the overhead ?
hunt
well the above solution doesn't work as it add autocomplete object everytime
hunt