views:

4685

answers:

4

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:

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

Both look kinda cumbersome.

+1  A: 

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');
        }
    });
});
Sbm007
Yes, this is similar to what I suggested in option #2 in my question. I prefer closures since using a global will make this function work only for one textbox.Anyway, it seems weird jquery doesn't have a simpler way of doing this.
noam
It is indeed weird, but I doubt there is a better way of doing it.You could use setInterval and then check if content has been changed every 0.1 sec and then do something. This will also include mouse pastes and so on. But it doesn't sound too elegant.
Sbm007
It's still possible that the content can be changed without a keystroke, for example by pasting with the mouse. If you care about this, you could theoretically catch mouse events too, but that setup is even uglier. On the other hand, if you're willing to ignore mouse-driven changes, this will do.
Adam Bellaire
A: 

do you consider using change event ?

$("#myTextBox").change(function() { alert("content changed"); });
Canavar
AFAIK, it is triggered only when the focus of the element is lost. Someone who is familiar with closures probably knows about this one. :)
simon
yes, this won't do - the change event is called only when the textbox loses focus. I want to handle the change right after the key was pressed.
noam
+4  A: 

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.

bobince
+1  A: 

I'd like to ask why you are trying to detect when the content of the textbox changed in real time?

An alternative would be to set a timer (via setIntval?) and compare last saved value to the current one and then reset a timer. This would guarantee catching ANY change, whether caused by keys, mouse, some other input device you didn't consider, or even JavaScript changing the value (another possiblity nobody mentioned) from a different part of the app.

DVK
my situation (which led me to this question) is that i need to show an 'Update Cart' button when the quantity in a textbox is changed. the user has no idea they need to lose focus and may otherwise not see the button.
Simon_Weaver
Sorry, I'm not sure that this answers the question of "why not check for diffs on a schedule". Just check often enough (every 1/4th of a second) and the user won't know any difference.
DVK