Sunday, 15 September 2013

How to init typeahead on new created input?

How to init typeahead on new created input?

I'm looking for a while to make it works.
This is my last try.
I have this html :
<td>
<input type="text" value="" class="typeahead" style="width: 310px"
autocomplete="off" data-provide="typeahead" />
<input type="hidden" value="" class="delivery" name="delivery[]" />
</td>
And on JS side, I have this :
$(".typeahead").on("create", function() {
$(this).typeahead({
limit : 20,
remote : {
url: '/ajax/places/?query=',
replace: function(url, encodedUrl) {
return url + encodedUrl + '&categorySon=5';
},
filter: function (result) {
var list = [];
result.aaData.map(function (item) {
list.push({
value: item.name + ' (' + item.code + ')',
id: item.id
});
});
return list;
}
}
}).bind('typeahead:selected', function (obj, datum) {
$(this).parent().parent().find("input[type='hidden']").val(datum.id);
});
});
So, when I add dynamycly an other input typeahead it doesn't works (even
on the one create on loading of the page, it works fine with basic
$(".typeahead).typeahead...)
thanks a lot for your help !

No comments:

Post a Comment