Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter bootstrap typeahead unable to add to dynamically created element

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?

like image 858
Adam Levitt Avatar asked Jun 14 '12 16:06

Adam Levitt


2 Answers

This solution requires no additional javascript. From https://stackoverflow.com/a/15094730

$("#element-that-will-contain-typeaheads").on("DOMNodeInserted", function () {
    $(this).find(".typeahead").typeahead();
});
like image 68
Dustin L. Avatar answered Nov 10 '22 07:11

Dustin L.


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

$("#job_title").livequery("create", function() {
    $(this).typeahead({
        source: searchFunction,        
        onselect: function(obj) {            
        }
    });
});
like image 45
Adam Levitt Avatar answered Nov 10 '22 06:11

Adam Levitt