How to detect a textbox’s content has changed

How to detect a textbox’s content has changed

I want to detect whenever a textbox’s content has changed. I can use the keyup method, but that will also detect keystrokes which do not generate letters, like the arrow keys. I thought of two methods of doing this using the keyup event:

Check explictly if the ascii code of the pressed key is a letter\backspace\delete
Use closures to remember what was the text in the textbox before the key stroke and check whether this has changed.

Both look kinda cumbersome.

Solutions/Answers:

Solution 1:

Start observing ‘input’ event instead of ‘change’.

jQuery('#some_text_box').on('input', function() {
    // do your stuff
});

…which is nice and clean, but may be extended further to:

jQuery('#some_text_box').on('input propertychange paste', function() {
    // do your stuff
});

Solution 2:

Use the onchange event in HTML/standard JavaScript.

In jQuery that is the change() event. For example:

$('element').change(function() { // do something } );

EDIT

After reading some comments, what about:

$(function() {
    var content = $('#myContent').val();

    $('#myContent').keyup(function() { 
        if ($('#myContent').val() != content) {
            content = $('#myContent').val();
            alert('Content has been changed');
        }
    });
});

Solution 3:

The ‘change’ event doesn’t work correctly, but the ‘input’ is perfect.

$('#your_textbox').bind('input', function() {
    /* This will be fired every time, when textbox's value changes. */
} );

Solution 4:

How about this:

< jQuery 1.7

$("#input").bind("propertychange change keyup paste input", function(){
    // do stuff;
});

> jQuery 1.7

$("#input").on("propertychange change keyup paste input", function(){
    // do stuff;
});

This works in IE8/IE9, FF, Chrome

Solution 5:

Use closures to remember what was the text in the checkbox before the key stroke and check whether this has changed.

Yep. You don’t have to use closures necessarily, but you will need to remember the old value and compare it to the new.

However! This still won’t catch every change, because there a ways of editing textbox content that do not involve any keypress. For example selecting a range of text then right-click-cut. Or dragging it. Or dropping text from another app into the textbox. Or changing a word via the browser’s spell-check. Or…

So if you must detect every change, you have to poll for it. You could window.setInterval to check the field against its previous value every (say) second. You could also wire onkeyup to the same function so that changes that are caused by keypresses are reflected quicker.

Cumbersome? Yes. But it’s that or just do it the normal HTML onchange way and don’t try to instant-update.

Solution 6:

$(document).on('input','#mytxtBox',function () { 
 console.log($('#mytxtBox').val());
});

You can use ‘input’ event to detect the content change in the textbox. Don’t use ‘live’ to bind the event as it is deprecated in Jquery-1.7, So make use of ‘on’.