When text change events do not fire?

I rely on a change event that fires whenever the contents of a text field change. According to jquery docs , this does not start until the text field loses focus. Under what circumstances cannot an event be triggered? Will cloning a page, go back, click on a link, etc., always make the text field lose focus and quit?

Or, to put it another way, is it possible to leave a page without the focus of textarea, which is losing input?

+4
source share
5 answers

In various tests, the change event does not fire if:

  • the user clicks on any browser button (the "Back", "Refresh" button, the "Home" button ...) (Chrome, Firefox amd Opera in Linux, but is activated when the address bar is clicked)
  • user uses shortcuts (f5, ctrl + u ...) (tested in Chrome + Firefox + Opera).

Here you can check an example: http://jsfiddle.net/Atinux/S6TkP/

Therefore, I recommend that you use the additional keyUp and keyDown events for your input forms and textarea.

+3
source

I would like to move on to defining the W3c event.

[http://www.w3.org/TR/DOM-Level-2-Events/events.html] [1]

The change event occurs when the control loses input focus and its value has been changed since the focus was received. This event is valid for INPUT , SELECT and TEXTAREA .

  • Bubbles: Yes
  • Canceled: No
  • Contextual Information: No

At any time when he loses focus, he should be fired.

+2
source

In Safari and Chrome, whenever you change text, the CHANGE event occurs. However, if you delete all text, the CHANGE event will no longer fire. This is mistake. That's why people use KEYPRESS events as a workaround.

+1
source

The event will be fired, but you cannot listen to it reliably. Form elements lose focus when the page is unloaded, but there is a race condition when it even works, and when listeners are called. If your listener is called before the page is unloaded, you can get a callback, but this is far from guaranteed.

If you want to track changes in textarea more reliably, you'd better listen to the keypress event.

0
source

It is better to use the keyUp / keyDown event, because the change event is fired only when the text / input has been changed and the element has lost focus.

0
source

Source: https://habr.com/ru/post/973130/


All Articles