Adding a listener to an element that is dynamically added

I have a javascript / jQuery page where I use Twitter Bootstrap typeahead. There are two separate places in javascript where I add elements that I would like to be typeaheads. Without asking subjective answers, what is a good way for me to dynamically register input as typeaheads? Give them all the different identifiers and sign up every time I add them? Is there any magic that can be done with .on()?

+1
source share
2 answers

You can use delegation jQueryfor this. For instance:

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

, typeahead, datepicker etc

, , bootstrap typeahead

, - :

 //add this in your add button click handler
 $tinput = $('<input type="text" name="bootstrap" class="typeahead"/>');

 //apply bootstrap typeahead
 $tinput.typeahead();

, , :)

0

All Articles