Aeseir
Aeseir

Reputation: 8424

Enable autocomplete when loaded by AJAX

trying to get my head around this problem:

I often load fragments of page via .load() function such as:

<div class="fragment_load">
    <form>
    <input id="typeaway" type="text" class="autocomplete"/>
    <label>Test Type</label>
    </form>

    <script type="text/javascript">
    $("#typeaway").autocomplete({
        serviceUrl: "/restaway",
        minChars: 3,
        paramName: "query",
    });
    </script>
</div>

Now as you can imagine autocomplete is not working due to this.

I am unsure how to make it work going forwards for these small fragments that i load via AJAX.

Any assistance is appreciated.

Upvotes: 0

Views: 48

Answers (2)

Anand Systematix
Anand Systematix

Reputation: 632

You can try below code:-

Just put it inside document.ready.

$(document).ready(function () {
 $("#typeaway").autocomplete({
        serviceUrl: "/restaway",
        minChars: 3,
        paramName: "query",
    });
 });

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337610

In this case you could put the autocomplete initialisation code within the callback of the load() method, something like this:

$('#foo').load('bar.html', function() {
    $("#typeaway").autocomplete({
        serviceUrl: "/restaway",
        minChars: 3,
        paramName: "query",
    });
});

Upvotes: 1

Related Questions