1

Is there a way to fire an event every time I change the text on an <input type="text"> when the changes are not made pressing keys on the keyboard?

What I mean is I want to capture when somebody type in, right-click -> paste or drag a selected text on an input and not only when a key is pressed.

I've tried:

<input id="some-id" onchange="alert('fired')">

and

$('#some-id').on('change', ev_handler);
$('#some-id').change(ev_handler);

the only event that fires are the ones related to key press but they don't fire on pasting or dragging.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ButterDog
  • 5,115
  • 6
  • 43
  • 61

2 Answers2

3

oninput event could met your needs, but just note it's not well supported in older browsers. You can find more info on Dottoro.

If you need to cover all browsers and all use cases like you mentioned, there is really no better way then periodically check (for example every 500ms) your input value with JavaScript.

You can find discussion about that here on StackOverflow.

Community
  • 1
  • 1
Boris Brdarić
  • 4,674
  • 2
  • 24
  • 19
-2

Try the onChange event, that should be issued everytime the input is changed, no matter how.

Hans Hohenfeld
  • 1,729
  • 11
  • 14
  • for some reason, neither this or any jQuery methods (.on() or .changed()) work on inputs type="text" – ButterDog Aug 01 '12 at 16:51
  • 1
    onchange only happens when tabbing out of the text box (or it otherwise loses focus), not immediately when input is entered. – Ghopper21 Aug 01 '12 at 16:52
  • @rushkeldon true, but that information was added AFTER I've posted my answer. Check the version history of the question... – Hans Hohenfeld Mar 29 '16 at 20:00