Twitter bootstrap typeahead cannot add dynamically generated element

Is there a way to convert a function like twart bootstrap to a function $ ("body"). on ()? The following code works fine if the element exists when the page loads.

$("#job_title").typeahead({
    source: searchFunction,        
    onselect: function(obj) {            
    }
});

But if I add text input with id = "job_title" dynamically, the above function does not work. Is there a workaround for this?

+5
source share
2 answers

I downloaded jquery.livequery.js to get the behavior I wanted:

$("#job_title").livequery("create", function() {
    $(this).typeahead({
        source: searchFunction,        
        onselect: function(obj) {            
        }
    });
});
+1
source

This solution does not require additional javascript. From fooobar.com/questions/1116643 / ...

$("#element-that-will-contain-typeaheads").on("DOMNodeInserted", function () {
    $(this).find(".typeahead").typeahead();
});
+3

All Articles