Aeseir Aeseir - 1 month ago 5
Ajax Question

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.

Answer

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",
    });
});