Search code examples
jqueryautocompletelive

Apply autocomplete on loaded inputs


I always have problems with the function live() and I still don't understand how to use it. (I can make it work with the datepicker but not with the autocomplete)

I'd like to apply this script on loaded inputs:

$("input.autocomp").autocomplete({
source: ["example 1", "example 22", "example 33",]
});

The script works properly on the existing inputs.

Could anyone help me?

Thanks


PS: This script works properly with loaded inputs.

    $(function(){
$('input.datepicker-inline').live('click', function() {
    $(this).datepicker({
        showOn:'focus',
        showButtonPanel: true,  
        changeMonth: true, 
        changeYear: true, 
        firstDay: 1, 
        dateFormat: 'yy-mm-dd',/*'dd/mm/yy'*/       
    }).focus();
});
})

Solution

  • Try something like this:

    $(".autocomp:not(.ui-autocomplete)").live('focus',function(){
          $(this).autocomplete(options).focus();
          return false;
    });
    

    Edit: you also have an extra comma in your array which will cause the code to fail in some browsers.

    In more recent versions of jquery, replace .live with .on:

    $(document).on('focus',".autocomp:not(.ui-autocomplete)",function(){
          $(this).autocomplete(options).focus();
          return false;
    });