Is there a way to convert twitter bootstrap's typeahead function into a $("body").on()
function? The following code works fine if the element exists on the page load.
$("#job_title").typeahead({
source: searchFunction,
onselect: function(obj) {
}
});
But if I add the text input with id="job_title"
dynamically, the above function doesn't work. Is there a workaround for this?
Is there a way to convert twitter bootstrap's typeahead function into a $("body").on()
function? The following code works fine if the element exists on the page load.
$("#job_title").typeahead({
source: searchFunction,
onselect: function(obj) {
}
});
But if I add the text input with id="job_title"
dynamically, the above function doesn't work. Is there a workaround for this?
typeahead
on the element when you add it?
– Esailija
Commented
Jun 14, 2012 at 16:28
.typeahead
on it. Because jsfiddle/9DxLZ/1 is working just fine.
– Esailija
Commented
Jun 14, 2012 at 16:36
This solution requires no additional javascript. From https://stackoverflow./a/15094730
$("#element-that-will-contain-typeaheads").on("DOMNodeInserted", function () {
$(this).find(".typeahead").typeahead();
});
I downloaded jquery.livequery.js to get the behavior I wanted:
$("#job_title").livequery("create", function() {
$(this).typeahead({
source: searchFunction,
onselect: function(obj) {
}
});
});
var fx = {
"init":function() {
$("#id_of_an_element").typeahead({
minLength:2,
delay:1 ...
})
},
"method2":function(){
some content;
}
}
Every time the element containing the typeahead is loaded into the DOM (eg. via AJAX success, call fx.init();