I want to execute a function every time the value of a specific input box changes. It almost works with $('input').keyup(function)
, but nothing happens when pasting text into the box, for example. $input.change(function)
only triggers when the input is blurred, so how would I immediately know whenever a text box has changed value?

- 6,547
- 4
- 28
- 40
-
2So why no bind both `keyup` and `paste` events? – Ilia G Jan 05 '12 at 18:08
-
http://www.devcurry.com/2009/07/detect-copy-paste-and-cut-operations-on.html -- Detecting cut/copy/paste events – BeRecursive Jan 05 '12 at 18:09
-
2@liho1eye `paste` is just one that I thought of, I'd rather listen for a definitive change than have to think of all the different incoming paths. – Jeriko Jan 05 '12 at 18:13
-
@Jeriko That is the only two ways value can be changed (beside the obvious updating via js code). – Ilia G Jan 05 '12 at 18:47
-
You also want to capture Ctlr-X (`cut`). – Alexis Wilke Jan 21 '15 at 00:21
-
ctrl+x and ctrl+v is detected by keyup but pasting can be done mouse as well hence both needed. – user1651602 Aug 05 '17 at 06:40
11 Answers
Update - 2021
As of 2021 you can use input
event for all the events catering input value changes.
$("#myTextBox").on("input", function() {
alert($(this).val());
});
Original Answer
just remember that 'on' is recommended over the 'bind' function, so always try to use a event listener like this:
$("#myTextBox").on("change paste keyup", function() {
alert($(this).val());
});

- 669
- 7
- 22

- 8,808
- 1
- 35
- 29
-
Why `paste` though? First i thought it does only work if we paste something with `CTRL+V`. – Black Sep 02 '16 at 13:57
-
7@NicolasS.Xu: direct asignation of values doesn't trigger any DOM event – Alejandro Silva Feb 09 '17 at 23:40
-
4Would be even better if using `console.log()` instead of `alert`. Would be very annoying for keyup to `alert`. – cytsunny May 29 '17 at 10:13
-
5@cytsunny yea sure, alert is annoying, but in this case is an example of the code to be executed on the value change, it really can be any payload you want. – Alejandro Silva May 30 '17 at 02:04
-
If i am changing value of input by a function then it is not showing any alert. please help. @AlejandroSilva – Jay Momaya May 23 '18 at 10:08
-
1If you right click and paste, this will return the value of the input prior to the paste. – alstr Nov 18 '18 at 07:42
-
-
I edited the answer, to permits scripts and right click and paste just add `input`. `"change paste keyup input"` – Heichou Sep 07 '20 at 12:01
-
2Just noticed the function is performed many times, just `"input"` is required without adding anything else – Heichou Sep 07 '20 at 12:37
Description
You can do this using jQuery's .bind()
method. Check out the jsFiddle.
Sample
Html
<input id="myTextBox" type="text"/>
jQuery
$("#myTextBox").bind("change paste keyup", function() {
alert($(this).val());
});
More Information
Try this.. credits to https://stackoverflow.com/users/1169519/teemu
for answering my question here: https://stackoverflow.com/questions/24651811/jquery-keyup-doesnt-work-with-keycode-filtering?noredirect=1#comment38213480_24651811
This solution helped me to progress on my project.
$("#your_textbox").on("input propertychange",function(){
// Do your thing here.
});
Note: propertychange for lower versions of IE.

- 2,812
- 4
- 20
- 28

- 161
- 1
- 3
you can also use textbox events -
<input id="txt1" type="text" onchange="SetDefault($(this).val());" onkeyup="this.onchange();" onpaste="this.onchange();" oninput="this.onchange();">
function SetDefault(Text){
alert(Text);
}

- 1
- 1

- 8,063
- 9
- 48
- 75
Try this:
Basically, just account for each event:
Html:
<input id = "textbox" type = "text">
Jquery:
$("#textbox").keyup(function() {
alert($(this).val());
});
$("#textbox").change(function() {
alert($(this).val());
});

- 22,405
- 23
- 76
- 114
$("#myTextBox").on("change paste keyup select", function() {
alert($(this).val());
});
select for browser suggestion

- 39
- 4
DON'T FORGET THE cut
or select
EVENTS!
The accepted answer is almost perfect, but it forgets about the cut
and select
events.
cut
is fired when the user cuts text (CTRL + X or via right click)
select
is fired when the user selects a browser-suggested option
You should add them too, as such:
$("#myTextBox").on("change paste keyup cut select", function() {
//Do your function
});

- 899
- 9
- 25
Try this:
$("input").bind({
paste : function(){
$('#eventresult').text('paste behaviour detected!');
}
})

- 535
- 7
- 21
Use this: https://github.com/gilamran/JQuery-Plugin-AnyChange
It handles all the ways to change the input, including content menu (Using the mouse)

- 7,090
- 4
- 31
- 50
-
How do You use it? I include it in my HTML and used $("input").anyChange(function (e) { console.log(e); }); But it never got to the console.log line – Ofer Gal Jul 05 '19 at 15:51
This combination of events worked for me:
$("#myTextBox").on("input paste", function() {
alert($(this).val());
});

- 796
- 7
- 11
You can use the following code to detect input, keychange, and keypress.
$("#myTextBox").on("input", "keyup", "keydown", "keypress" function() {
alert($(this).val());
});

- 2,324
- 26
- 22
- 31

- 15
- 7