开发者

How to get the old value of a textarea

开发者 https://www.devze.com 2023-02-15 08:54 出处:网络
That\'s it. How can I get the oldvalue of a textarea in order to compare it with a new value?. The current value becomes the old value after triggering an event (say, a keyup event).

That's it. How can I get the old value of a textarea in order to compare it with a new value?. The current value becomes the old value after triggering an event (say, a keyup event).

I have seen some solutions, for example, using cookies to record the old value and the new value, however, such solution doesn't work in my case because of the type of manipulation I want to perform later.

Hopefully, there is a suggestion which works better than that.

UPDATE:

After following some suggestions from @drachenstern, @Matthew, @Peter, I ended up with something like this

var startTimer = null;
var oldValue;
$("#textarea").keydown($.debounce( 1000, true, function(){
oldValue = $("#textarea").val();
}
));

$("#textarea").keyup(function(){
if(开发者_如何转开发startTimer) clearTimeout(startTimer);
startTimer = setTimeout(function(){
var newValue = $("#textarea").val();
d = // here a clever comparison
oldValue = newValue;
},2000);
})

By the way, $.debounce is a function from the jQuery throttle / debounce plugin.

This does exactly what I want, however, I'd like to obtain the variable d outside of setTimeout function and of keyup function, in order to use it elsewhere. However, clearTimeout seems to be tricky when it comes to return values. Any way to get the value of the d?.


var oldValue = document.getElementById("textareaid").value;

document.getElementById("textareaid").onchange = function(){
  var newValue = this.value;
  //Compare oldValue and newValue
  oldValue = newValue;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消