Can jQuery check whether input content has changed?
There is a simple solution, which is the HTML5 input
event. It's supported in current versions of all major browsers for <input type="text">
elements and there's a simple workaround for IE < 9. See the following answers for more details:
- jQuery keyboard events
- Catch only keypresses that change input?
Example (except IE < 9: see links above for workaround):
$("#your_id").on("input", function() {
alert("Change to " + this.value);
});
Yes, compare it to the value it was before it changed.
var previousValue = $("#elm").val();
$("#elm").keyup(function(e) {
var currentValue = $(this).val();
if(currentValue != previousValue) {
previousValue = currentValue;
alert("Value changed!");
}
});
Another option is to only trigger your changed function on certain keys. Use e.KeyCode
to figure out what key was pressed.