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.
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.
But if I add the text input with id="job_title" dynamically, the above function doesn't work. Is there a workaround for this?
javascript
jquery
twitter-bootstrap
function
jquery-events
Share
edited Aug 10, 2019 at 16:53
Brian Tompsett - 汤莱恩
5,8937272 gold badges6161 silver badges133133 bronze badges
asked Jun 14, 2012 at 16:23
Adam LevittAdam Levitt10.5k2727 gold badges8989 silver badges145145 bronze badges14
2Fix the missing quote before someone thinks that's the problem
– Esailija
CommentedJun 14, 2012 at 16:25
Oops, yeah that was just a bad paste error. Thanks for pointing that out. but this is not the issue :)
– Adam Levitt
CommentedJun 14, 2012 at 16:26
Is it a problem to initialize the typeahead on the element when you add it?
– Esailija
CommentedJun 14, 2012 at 16:28
Nope, this code works perfectly if the #job_title element is already on the page inside the HTML. This code only does not work if I dynamically add the #job_title element.
– Adam Levitt
CommentedJun 14, 2012 at 16:29
1You need to add the code how you create the element and consequently call .typeahead on it. Because jsfiddle/9DxLZ/1 is working just fine.
– Esailija
CommentedJun 14, 2012 at 16:36
|
Show 9 more ments
3 Answers
3
Reset to default
3
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: