Answer the question
In order to leave comments, you need to log in
Plugin event not firing when element is added after page load?
This question is not related to Bootstrap TouchSpin in any way , but the example is made on it, I think the same situation will be with another plugin.
Essence of the question:
There is a page with elements (in our case, it is a Bootstrap TouchSpin spinner), some of them are in the code when the page is loaded, and some are loaded by ajax depending on the content. The situation is that events for changes (any events) do not respond to elements added after the page is loaded.
Approximate example (working example: https://jsfiddle.net/atach/s210gr6y/18/ ):
// Инициализирутся элементы которые на странице
initSpinner();
setTimeout(function() {
// При какиих то условиях подгружаются и иногда добавляются еще
$(".newSpinner").html('<hr/><input class="spinner" value="1" readonly /><br/>');
// Повторно инициализирую спиннер
initSpinner();
}, 500);
$(".spinner").on("touchspin.on.stopspin", function(target) {
alert("On change")
});
function initSpinner() {
$("input.spinner").trigger('touchspin.destroy');
$("input.spinner").TouchSpin();
}
With this code, the Change event does not fall on new elements. $(".spinner").on("touchspin.on.stopspin", function(target) {
alert("On change")
});
Answer the question
In order to leave comments, you need to log in
$("body").on("touchspin.on.stopspin", ".spinner", function(target) {
alert("On change")
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question