Home » Jquery » What is the difference between “change” and “input” event for an INPUT element

What is the difference between “change” and “input” event for an INPUT element

Posted by: admin November 29, 2017 Leave a comment

Questions:

Can someone tell me what the difference between the change and input events is?

I am using jQuery for adding them:

$('input[type="text"]').on('change', function() {
    alert($(this).val());
})

It also works with input instead of change.

Maybe some difference in the event ordering relative to focus?

Answers:

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. The onchange attribute can be used with: <input>, <select>, and <textarea>.

I’ve created a demo that demonstrates these things.

Questions:
Answers:
  • The change event fires in most browsers when content is changed and
    the element loses focus, basically an aggregate of changes. You will not see this fired for every single changed unlike the input event.

  • The input event fires synchronously on change of the content for the
    element. So you will see this event occur more often. Browser support varies.