Reputation: 1083
I have an input field where the value is populated using a modal, simply when i click a button a modal will appear and i will select something there and it will populate my input field.
now my problem is that I want to trigger an event whenever the value of my input field changes. I already tried using .change and .blur but it wont trigger.
this is all I tried:
$('#myTextbox').on('input', function() {
alert("test");
});
$('#myTextbox').on('change', function() {
alert("test");
});
$('#myTextbox').on('blur', function() {
alert("test");
});
Upvotes: 0
Views: 3688
Reputation: 6628
Use event delegation
You question says that you input field is generated dynamically. So it was not in the DOM when your page gets ready.
$('body').on('blur', '#myTextbox', function() {
alert("test");
});
FYI, you can use change
as well.
EDIT
One solution you can use setTimeout
till your text-box gets value populated.
OR
You can try with DOMAttrModified
$('body').on('DOMAttrModified propertychange paste', '#myTextbox', function() {
alert("test");
});
Upvotes: 2
Reputation: 1368
Try using like this, might be your input
field is populating at run time, that's why the event is not firing.
$(document).on('change blur keyup', '#myTextbox', function() {
alert("test");
});
You can bind all the event in one .on()
method. No need write no of times.
Upvotes: 0
Reputation: 863
$(document).ready(function() {
$('#myTextbox').on('blur', function() {
alert("test");
});
});
OR
$(document).ready(function() {
$('#myTextbox').on('change', function() {
alert("test");
});
});
Upvotes: 1