Paul Biggar
Paul Biggar

Reputation: 28769

When do textarea change events not get triggered?

I'm relying on the change event being triggered whenever the contents of the textarea changes. According to the jquery docs, this doesn't get triggered until the textarea loses focus. Under what circumstances could the event not be triggered? Would clsoing a page, going back, clicking on a link, etc, always cause the textarea to lose focus and be fired?

Or to put it another way, is it possible to leave a page without a focusses textarea losing input?

Upvotes: 5

Views: 7413

Answers (5)

adrian
adrian

Reputation: 2376

I would go to the W3c definition of the event.

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

The change event occurs when a control loses the input focus and its value has been modified since gaining focus. This event is valid for INPUT, SELECT, and TEXTAREA element.

  • Bubbles: Yes
  • Cancelable: No
  • Context Info: None

Any time it loses focus, it should be fired.

Upvotes: 2

yazz.com
yazz.com

Reputation: 58796

On Safari and Chrome whenever you change the text the CHANGE event is fired. However, if you delete all the text then the CHANGE event is not fired anymore. This is a bug. This is why people use the KEYPRESS events as a workaround

Upvotes: 1

Atinux
Atinux

Reputation: 1703

With differents tests, the change event is not triggered when:

  • the user clicks on any button of the browser (back button, refresh, home button...) (Chrome, Firefox amd Opera on Linux but triggered when click on adress bar)
  • the user uses shortcuts (f5, ctrl+u...) (tested on Chrome + Firefox + Opera).

You can test an example here: http://jsfiddle.net/Atinux/S6TkP/

So I recommand you to use in addition keyUp and keyDown events on your input and textarea forms.

Upvotes: 3

Andreas Köberle
Andreas Köberle

Reputation: 110972

Its better to use keyUp/keyDown event cause the change event only fires when the textare/input was changed and the element lost its focus.

Upvotes: 0

mustpax
mustpax

Reputation: 1408

The event will be triggered but you won't be able to listen to it reliably. Form elements lose focus when a page is unloading but there is a race condition around when the even gets triggered and when the listeners get called. If your listener is called before the page is done unloading, you might get the callback but it's far from guaranteed.

If you want to be able to keep track of changes to a textarea more reliably you are better off listening to the keypress event.

Upvotes: 0

Related Questions