javascript - jQuery keyup only keys that affects textarea content - Stack Overflow

How can I detect if the value of a textarea changes using jQuery? I'm currently using keyup() but

How can I detect if the value of a textarea changes using jQuery? I'm currently using keyup() but this triggers every key stroke of course, I dont want my code to run if it's an arrow key that was pressed or any other key that doesn't have an impact on the value of the textarea.

Take a look:

$('textarea').keyup(function() {
     if (content was changed)
         // Do something
});

I hope you understand. How can I do this the best way? I don't want to pare the current value to an old value to check for changes, I hope that's not the only way.

How can I detect if the value of a textarea changes using jQuery? I'm currently using keyup() but this triggers every key stroke of course, I dont want my code to run if it's an arrow key that was pressed or any other key that doesn't have an impact on the value of the textarea.

Take a look:

$('textarea').keyup(function() {
     if (content was changed)
         // Do something
});

I hope you understand. How can I do this the best way? I don't want to pare the current value to an old value to check for changes, I hope that's not the only way.

Share Improve this question asked Feb 12, 2013 at 11:18 lawlslawls 1,5183 gold badges21 silver badges35 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

By all means the easiest way is to store old values to data and do the check every keyup. The solution is quite short and will work in any case. No need to reinvent the wheel.

$("textarea").data("oldValue", function() {
    return this.value;
}).keyup(function() {
    var $this = $(this);
    if (this.value !== $this.data("oldValue")) {
        // Do something

        $this.data("oldValue", this.value);
    }
});

DEMO: http://jsfiddle/vvbSj/

$('textarea').blur(function() {
  //This will be invoked when the focus is removed
});

$('textarea').change(function() {
  //Same as the blur
});

Is this what you want

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745626481a4636838.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信