at.
at.

Reputation: 52590

How to tell when an HTML textarea has been changed by Javascript

A widget I'm using modifies an HTML textarea element. I need to know when that element has been modified and preferably I'd like to actually hide that element as well. I'm using jQuery, so I naturally tried the $('#textarea_id').change() event. But it's never triggered because I guess the textarea never loses focus.

What's the best way to monitor that textarea, preferably hidden (CSS has display:none)? Please don't tell me setInterval...

Upvotes: 1

Views: 3388

Answers (2)

Greg
Greg

Reputation: 2229

I would use .keyup(). If you don't want the event to fire every keypress, have it restart a half second timer every keypress and have the timer trigger the things you want to happen when the user stops typing. You can play with the time to get it to your liking.

Upvotes: 0

hunter
hunter

Reputation: 63562

You could manage this with a global variable.

var text = "";

$('#textarea_id').bind("keyup paste", function(e){
    if ($(this).val() != text)
    {
        // text changed
        text = $(this).val();
    }    
});

working example: http://jsfiddle.net/n8keN/

Upvotes: 1

Related Questions