25

Does text input element not have a change event? When I attach a change event handler to a text input it is not being fired. Keyup is fired, but keyup is not sufficient for detecting a change as there are obviously other ways of entering text into an input.

jcvandan
  • 14,124
  • 18
  • 66
  • 103

6 Answers6

29

The HTML4 spec for the <input> element specifies the following script events are available:

onfocus, onblur, onselect, onchange, onclick, ondblclick, onmousedown, onmouseup, onmouseover, onmousemove, onmouseout, onkeypress, onkeydown, onkeyup

here's an example that bind's to all these events and shows what's going on http://jsfiddle.net/pxfunc/zJ7Lf/

I think you can filter out which events are truly relevent to your situation and detect what the text value was before and after the event to determine a change

MikeM
  • 27,227
  • 4
  • 64
  • 80
  • 1
    I thought of using change, focus, blur, keyup and mouseup all together - however mouseup only fires if the mouse is over the texbox, this means it doesn't pick up a user pasting from a context menu where paste is likely to be outside the scope of the text input – jcvandan May 26 '11 at 14:33
  • 1
    looks like newer browsers support `paste` and `input` events, I added those after finding them here, [jquery - How to detect right mouse click + paste using JavaScript?](http://stackoverflow.com/questions/441631/how-to-detect-right-mouse-click-paste-using-javascript)...heres the [updated fiddle](http://jsfiddle.net/pxfunc/zJ7Lf/1/) – MikeM May 26 '11 at 14:38
  • potentially useful but can't use them really if they don't have very wide support! – jcvandan May 26 '11 at 14:40
  • actually, `input` seems to be the one-stop shop at least in IE9/FF/Chrome – MikeM May 26 '11 at 14:42
  • 1
    updated fiddle: http://jsfiddle.net/zJ7Lf/288/ illustrating the issue with capturing programmatically triggered changes – Jonathan Dec 05 '12 at 02:09
23

I have found that this works:

$(document).ready(function(){
    $('textarea').bind('input propertychange', function() {
        //do your update here
    }

})
Charlie
  • 8,530
  • 2
  • 55
  • 53
Pow-Ian
  • 3,607
  • 1
  • 22
  • 31
  • I use it extensively to validate inline with ajax. I think the new mutation events being discussed will replace some of this functionality but this solution has been tested in Firefox, Chrome, IE 10 and Safari and is known to be working. – Pow-Ian Jul 02 '13 at 17:45
14

The change event only fires after the input loses focus (and was changed).

ventaur
  • 1,821
  • 11
  • 24
  • 3
    If you REALLY want to know when any text changes, pretty much as it changes, you're stuck polling the input. That is, use setInterval with a function that compares the current value of the input to a previously stored value (via some variable with higher scope than the function). If the two values do not match, a change occurred. Then, store the current value into that other variable for the next check. – ventaur May 26 '11 at 14:21
4

There is no real solution to this - even in the links to other questions given above. In the end I have decided to use setTimeout and call a method that checks every second! Not an ideal solution, but a solution that works and code I am calling is simple enough to not have an effect on performance by being called all the time.

function InitPageControls() {
        CheckIfChanged();
    }

    function CheckIfChanged() {
        // do logic

        setTimeout(function () {
            CheckIfChanged();
        }, 1000);
    }

Hope this helps someone in the future as it seems there is no surefire way of acheiving this using event handlers...

jcvandan
  • 14,124
  • 18
  • 66
  • 103
3

You can achieve it:

 $(document).ready(function(){              
     $('#textBox').keyup(function () {alert('changed');});
 });

or with change (handle copy paste with right click):

 $(document).ready(function(){              
     $('#textBox2').change(function () {alert('changed');});
 });

Here is Demo

Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
1
$('#input').on("input",function () {alert('changed');});

works for me.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
Yaseer Arafat
  • 81
  • 1
  • 4