Each answer is missing some points, so here is my solution:
$("#input").on("input", function(e) {
var input = $(this);
var val = input.val();
if (input.data("lastval") != val) {
input.data("lastval", val);
//your change action goes here
console.log(val);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="input">
<p>Try to drag the letters and copy paste</p>
The Input Event
fires on Keyboard input, Mouse Drag, Autofill and Copy-Paste tested on Chrome and Firefox.
Checking for previous value makes it detect real changes, which means not firing when pasting the same thing or typing the same character or etc.
Working example: http://jsfiddle.net/g6pcp/473/
update:
And if you like to run your change function
only when user finishes typing and prevent firing the change action several times, you could try this:
var timerid;
$("#input").on("input", function(e) {
var value = $(this).val();
if ($(this).data("lastval") != value) {
$(this).data("lastval", value);
clearTimeout(timerid);
timerid = setTimeout(function() {
//your change action goes here
console.log(value);
}, 500);
};
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="input">
If user starts typing (e.g. "foobar") this code prevents your change action
to run for every letter user types and and only runs when user stops typing, This is good specially for when you send the input to the server (e.g. search inputs), that way server does only one search for foobar
and not six searches for f
and then fo
and then foo
and foob
and so on.