Attach JQuery Autocomplete to a text field which is loaded by Ajax call
NickName:Muhammad Ummar Ask DateTime:2011-05-11T23:20:22

Attach JQuery Autocomplete to a text field which is loaded by Ajax call

I have a simple web application in which I have created a wizard, each wizard page contains different form fields which are populated from database, as user presses next the page data is retrieved from server using Ajax call. Here is the code of the page which is retrieved from server against an Ajax call. I am making it simple to understand..

function printAdAlertWizardStep($step)
    {
            switch($step)
            {
                case 1: //step of wizard, first step
                    print "Welcome to alert wizard,...";
                    break;
                case 2: //second step of wizard which contains the text field to which I want to attach autocomplete list.
                ?>
<table>
        <tr>
        <td>Keywords</td>
        <td><!-- This is text field to which I want to attach autocomplete -->
        <input id="nalertkw" name="nalertkw" size="10" type="text">
        </td>
    </tr>
</table>

            <?php
            break;
            }
        }
    }

And the Java script code for attaching Autocomplete to keywords text field is

//Script will be executed when the page is ready for scripting
$(document).ready(function() {
var availableTags = [
            "ActionScript",
            "AppleScript",
            "Asp",
            "BASIC",
            "C",
            "C++",
            "Clojure",
            "COBOL",
            "ColdFusion",
            "Erlang",
            "Fortran",
            "Groovy",
            "Haskell",
            "Java",
            "JavaScript",
            "Lisp",
            "Perl",
            "PHP",
            "Python",
            "Ruby",
            "Scala",
            "Scheme"
        ];
        $( "#nalertkw" ).autocomplete({
            source: availableTags
        });
});

Now the problem is that the form is loaded after the user presses next and the $(document).ready() function has fired already when the #nalertkw text field doesn't exists. So the autocomplete is not working. I am using Jquery-UI Autocomplete, How can I attach the autocomplete to a textfield which is loaded through Ajax call?

edit: Moreover I have tested my setup on a simple page (without Ajax call) with textfield and attaching the autocomplete to that text field the same way. It works absolutely fine. It confirms that autocomplete setup is correct, but it don't works when attached to a textfield which is retrieved through Ajax call.

Copyright Notice:Content Author:「Muhammad Ummar」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/5966641/attach-jquery-autocomplete-to-a-text-field-which-is-loaded-by-ajax-call

Answers
Raghav 2011-05-11T15:41:20

Try hooking the event in Ajax call's :success handler, like this,\n\n$.ajax({\n url: \"page.aspx/FetchEmailList\",\n data: \"{ 'mail': '\" + request.term + \"' }\",\n dataType: \"json\",\n type: \"POST\",\n contentType: \"application/json; charset=utf-8\",\n dataFilter: function (data) { return data; },\n success: function (data) {\n $( \"#nalertkw\" ).autocomplete({\n source: availableTags\n });\n\n }\n }))\n }\n });\n",


Muhammad Ummar 2011-05-11T19:24:39

best solution I have found\n\n$(\"#nalertkw\").live(\"keydown.autocomplete\", function(){ $(this).autocomplete({ \nsource: availableTags \n}); \n}); \n\n\nas soon as the text filed is clicked the autocomplete is attached to it... some one replyed this solution to my question, but has deleted his answer.. I only make little changes to his code and it starts working, so I decided to add an answer to help others. Thanks to you all for your suggestions. @Raghav, solution also looks good, and I think it will work also.. so upvote for you @Raghav. Thanks.",


Femi 2011-05-11T15:31:59

After the new page is loaded: run your autocomplete call. Something like:\n\njQuery.post('[url]', '[arguments]', function(data, status){\n jQuery(\"[pageframe]\").html(data);\n var availableTags = [\n \"ActionScript\",\n \"AppleScript\",\n \"Asp\",\n \"BASIC\",\n \"C\",\n \"C++\",\n \"Clojure\",\n \"COBOL\",\n \"ColdFusion\",\n \"Erlang\",\n \"Fortran\",\n \"Groovy\",\n \"Haskell\",\n \"Java\",\n \"JavaScript\",\n \"Lisp\",\n \"Perl\",\n \"PHP\",\n \"Python\",\n \"Ruby\",\n \"Scala\",\n \"Scheme\"\n ];\n $( \"#nalertkw\" ).autocomplete({\n source: availableTags\n });\n}, \"html\");\n",


More about “Attach JQuery Autocomplete to a text field which is loaded by Ajax call” related questions

Attach JQuery Autocomplete to a text field which is loaded by Ajax call

I have a simple web application in which I have created a wizard, each wizard page contains different form fields which are populated from database, as user presses next the page data is retrieved ...

Show Detail

Is there a way to call abort() on a jQuery Autocomplete AJAX call?

I'm trying to build a "cancel" link to a jQuery autocomplete field for a large database call. In case it takes too long, it'd be nice if there were a way to cancel the AJAX call that the autocomple...

Show Detail

jQuery-UI Autocomplete Widget not binding to ajax loaded partial form in Rails

I have a form with a text field that uses jQuery-UI autocomplete widget. I'm loading this form as a partial using AJAX with Ruby on Rails &lt;li id="activity_&lt;%= activity.id %&gt;"&gt;&lt;%= li...

Show Detail

Using autocomplete (Jquery) on AJAX loaded partial view (MVC4)

I've got a view to create Project models which contains (among other things) a table of company-related data. I've added a button that does an AJAX call to retrieve a partial view and adds it to the

Show Detail

Jquery .autocomplete not working after ajax call

Hi guys i am trying to write a title matcher, i got it working but when an ajax call is made it no longer works below is my code : &lt;script type="text/javascript" src="jquery.autocomplete.js"&g...

Show Detail

Jquery Autocomplete with Ajax Call not working

Im adding enhancements to an existing rails 5.1 app. I need to add autocomplete with data retrieved from a Mysql database. The application has Jquery, simple_form, and materialize all included. My

Show Detail

ajax loaded content with autocomplete

I have tried to solve this problem and i am quite helpless now. I am loading data into a div via Ajax with: function sk_upload_l() { var xmlhttp; if (window.XMLHttpRequest) { xml...

Show Detail

JQuery Autocomplete on Ajax loaded content

I would like to use the JQuery's autocomplete plugin on a input box loaded using Ajax: I have tried to achieve that using the following code, however it only works when the users clicks twice into...

Show Detail

Ajax autocomplete jquery is not working when text field is not empty

I'm working with Ajax autocomplete for jquery and my HTML code is &lt;input type="text" name="autocomplete" id="globalBCCAutoComplete"&gt; and my autocomplete JS code is $(document).rea

Show Detail

jQuery focusout() with text field and autocomplete

I have a text field with Typeahead autocomplete. Users should be able to type in manually or choose from select list. Before the user can submit the form I already want to validate what the user has

Show Detail