M
M
Maxim Vasiliev2012-04-10 14:28:09
JavaScript
Maxim Vasiliev, 2012-04-10 14:28:09

How to track changes in the text of an input field?

The "change" event fires only after the focus is moved from the changed input.
And I want to immediately, as soon as the user climbed something to change.

$("input").on("edit", function() { <br>
  /* добавить в заголовок формы предупреждение "не сохранено" */<br>
  /* заблокировать закрытие окошка по ESC */<br>
});<br>

Answer the question

In order to leave comments, you need to log in

9 answer(s)
D
DemiurgeOrion, 2012-04-11
@qmax

There is a wonderful jQuery Text Change
plugin www.zurb.com/playground/jquery-text-change-custom-event

M
marsdenden, 2017-10-09
@marsdenden

I understand that a lot of time has passed, but for the future

$(document).on("input",function(ev){
  console.log($(ev.target).val());
});

catches all changes, including copy-paste

T
try4tune, 2012-04-10
@try4tune

onKeyPress?

L
ligaliga, 2015-11-17
@ligaliga

input event. It reacts right away:
https://learn.javascript.ru/events-change

A
Anatoly, 2012-04-10
@taliban

onfocus - save the length of the text in the field
onkeyUp - check the length of the text in the field, and if it is not equal, then the text has been changed.

M
mikenerevarin, 2012-04-11
@mikenerevarin

OnInput?

S
serso, 2012-04-10
@serso

api.jquery.com/change/

F
Fr3nzy, 2012-04-10
@Fr3nzy

I recently had to write something like this for some browser:

setInterval(function(){
    var curVal = $(".someInputs").val();
    var prevVal  = $(".someInputs").data("prevVal") || null;
    

    if (prevVal !== curVal) {
        // value changed
    }
}, 100)

And, I remembered, this is for the case when an insertion took place in the field in one way or another (ctrl + v, through the context menu, etc ...)

P
Pavel Isakov, 2012-04-19
@kwikpik

I would not forget about pasting text from the clipboard with the mouse.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question