Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<input> text change events

Is there a library that can extract all text events from an <input type=text> (or contentEditable -- any one-line input field) element?

In particular, I need to know whenever the text is changed by:

  • typing (asdf, backspace)
  • cut/paste
  • key combo actions (e.g. ctrl+bksp or option+bksp deletes the previous word)
  • dragged & dropped text
  • edit menu actions

And preferably what it was that changed (whether and what text was inserted, deleted, or replaced).

Needs to work on Chrome, Safari, Firefox 3+, IE9+.

like image 296
nornagon Avatar asked May 19 '11 09:05

nornagon


1 Answers

The HTML5 oninput event is supported by all those browsers and works very much like the onchange event, but fires as soon as the element's input changes. It also bubbles, so you can capture it further up the document tree.

element.oninput = function () {

}

Working demo: http://jsfiddle.net/Zfthe/

http://whattheheadsaid.com/2010/09/effectively-detecting-user-input-in-javascript

like image 52
Andy E Avatar answered Oct 08 '22 22:10

Andy E