Is there a jquery event to handle text field changes?

I am looking for a way to bind to an event whenever the text of a text field changes. Right now I have something like this:

$("#inputPane").change(function() { alert("Changed!"); }); 

However, this is only caused when the text field loses focus. I want to receive an event whenever a new character is added, deleted or cut / pasted.

I would prefer not to use third-party plugins for this. Using pure jquery would be nicer.

+4
source share
3 answers

here is an example of how to commit any change

 $('#Textbox1, #Textbox2').each(function () { $(this).bind("propertychange keyup input paste", function (event) { alert("changed"); }); }); 

I use this in a group of text fields and it only fires once

+2
source

The best option is to use keyup .

 $("#inputPane").keyup(function() { alert("Changed!"); }); 

However, you should keep in mind that it will fire every time you press ANY, even Ctrl , Alt , Shift , etc.

+3
source

You will probably need several events that will be displayed in characters, insert, etc.

 $("#inputPane").on('keyup paste click whatever', function() { alert("Changed!"); }); 

If for some reason it fires several times:

 var fire = true; $("#inputPane").on('keyup paste click whatever', function() { if (fire) { fire=false; alert("Changed!"); } setTimeout(function() {fire=true}, 200); }); 
+3
source

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


All Articles