Reputation: 196
I'm trying to remove an alert on a form when a user fixes an empty field. My generalized code works for all input elements where the user has to type in the field, but my input that has a popup calendar does not fire the "change" event if the date is placed in the field by the system. I thought the change event fired when the field lost its focus?
$("#my_input").change(function() {
$(".StatusBox").html("");
$(this).unbind("change");
});
I have another piece of code that runs after this and changes the input field(user clicking on a calendar date and therefore updating the input). Shouldn't that fire the change event?
Upvotes: 13
Views: 38517
Reputation: 5496
Use something like this, it always works.
$(function() {
$("#my_input").on("change paste keyup", function() {
alert($(this).val());
});
});
Upvotes: 9
Reputation: 40106
You can programmatically force a change event via the trigger('change')
method:
$('#elementID').trigger('change');
This assumes, of course, that $('#elementID')
is (still) bound to a change event...
Upvotes: 14
Reputation: 1462
It is possible that you are attaching the event before the element is loaded on the page. Maybe try doing this to let the page finish loading before attaching the event
$(function() {
$("#my_input").change(function() {
$(".StatusBox").html("");
$(this).unbind("change");
});
});
Upvotes: 2
Reputation: 818
Have you tried use $("#my_input").on('change', function ... ?
It works slightly different than bind, and you can put it in a function to be re-run for new objects created on demand (so, it don't need necessarily to be inside the "ready" statement).
Upvotes: 2