Difference between "change" and "input" event for an `input` element
The
change
event
fires in most browsers when content is changed and the element losesfocus
. It's basically an aggregate of changes. It will not fire for every single change as in the caseinput
event
.The
input
event
fires synchronously on change of the content for the element. As such, the event listener tends to fire more frequently.Different browsers do not always agree whether a change event should be fired for certain types of interaction
According to this post:
oninput
event occurs when the text content of an element is changed through the user interface.onchange
occurs when the selection, the checked state, or the contents of an element have changed. In some cases, it only occurs when the element loses the focus or when pressing return (Enter) and the value has been changed. The onchange attribute can be used with:<input>
,<select>
, and<textarea>
.
TL;DR:
oninput
: any change made in the text contentonchange
:- If it is an
<input />
: change + lose focus - If it is a
<select>
: change option
- If it is an
$("input, select").on("input", function () {
$("pre").prepend("\nOn input. | " + this.tagName + " | " + this.value);
}).on("change", function () {
$("pre").prepend("\nOn change | " + this.tagName + " | " + this.value);
}).on("focus", function () {
$("pre").prepend("\nOn focus | " + this.tagName + " | " + this.value);
}).on("blur", function () {
$("pre").prepend("\nOn blur | " + this.tagName + " | " + this.value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" />
<select>
<option>Alice</option>
<option>Bob</option>
<option>Carol</option>
<option>Dave</option>
<option>Emma</option>
</select>
<pre></pre>