0

I am trying to make an undo function in a textarea as I find the native one on IE "Have to use it at work" to be lacking.

The idea is to push to the array anytime the user presses space, backspace, delete, or they right click the mouse. Where I am having trouble is navigating through the array "history" with the Z key. I was thinking that if I make a counter increment every time the Z key is pressed then minus that from array.length it would allow me to move through the history.

Here is a JSFiddle I made for testing http://jsfiddle.net/synthet1c/5fqe3/2/

here is the part of the script I am having issues with

//keypress listener script

var pos = undo.length - 1

if(key_code == "90"){
var counter = 0
counter++;
var newCount = pos - counter;
e.preventDefault();
id("myTextarea").value = undo[newCount] + " ";
id('counter').innerHTML = counter;

}

Also any ideas on only pushing just the last word into the array when pressing spacebar to save on memory would be great.

Cœur
  • 37,241
  • 25
  • 195
  • 267
synthet1c
  • 6,152
  • 2
  • 24
  • 39
  • IE has a native "Undo" for TEXTAREAs? – Šime Vidas Dec 14 '12 at 02:21
  • 1
    I believe a [Stack](http://www.i-programmer.info/programming/javascript/1674-javascript-data-structures-stacks-queues-and-deques.html) would be better than an array (ie you can use _Array.push()_ and _Array.pop()_) [More reading here](http://www.i-programmer.info/programming/javascript/1674-javascript-data-structures-stacks-queues-and-deques.html) – jahroy Dec 14 '12 at 02:40
  • @jahroy I don't understand why you'd ever want to use that `Stack` data structure...all it does it limit the methods on an array. Just don't use any other methods and it's no different! – Ian Dec 14 '12 at 04:31
  • @Ian - Fair enough... I didn't actually read the page closely (my bad). I just figured it would explain the notion of a Stack if it was a new concept. My main point was just to use _push()_ and _pop()_ to maintain an undo stack. – jahroy Dec 14 '12 at 04:40
  • @jahroy No problem, I think that's a good point and I agree they could be used to make this program work, it just could've been explained better :) – Ian Dec 14 '12 at 04:49

2 Answers2

1

Well every keypress you reset your counter to 0 with var counter = 0, if you want to increment counter you should declare your var counter = 0; out of the keypress event. Also increment counter after your operations

http://jsfiddle.net/roine/5fqe3/4/

Jonathan de M.
  • 9,721
  • 8
  • 47
  • 72
  • Thanks for your response, I knew it would be something basic... although I did try setting counter globally, then using closure in the outside of the keypress functions.. But I must have done it wrong, so will have to go back and learn the basics. Thanks once again – synthet1c Dec 14 '12 at 06:48
0

Writing your own undo facility is a good exercise, but you might also want to consider existing solutions, such as those mentioned in this StackOverflow article

Community
  • 1
  • 1
jdigital
  • 11,926
  • 4
  • 34
  • 51