title: JS text input history on up arrow
body: <p>Im trying to build in a history in a text field, where the user may press the up arrow and see the previous text that was written in the field.</p>
  
  <p>So far i have managed to track previous inputs: <a href="http://jsfiddle.net/gLhkr3mL/" rel="nofollow">http://jsfiddle.net/gLhkr3mL/</a></p>
  
  <pre><code>var prevCommand = [];
  var commandCount = 0;
  var keyCount = 0;
  $("#form-prompt").on("submit", function (e) {
      commandCount++;
      keyCount = 0;
      prevCommand[commandCount] = $('#textTest').val();
      e.preventDefault();
      $('#textTest').val(''); 
  });
  
  
  $(document).keydown(function(event){
      if(event.which == 38 &amp;&amp; $("#textTest").is(":focus")){
          keyCount++;
          if(typeof prevCommand[keyCount] !== "undefined") {
              $('#res').append('&lt;hr&gt;'+prevCommand[keyCount]);
          }
      }else if(event.which == 40 &amp;&amp; $("#command-text").is(":focus")) {
  
      }
  });
  </code></pre>
  
  <p>The problem im facing is that in this case the history will be shown in the wrong order, as i want the newest input to come first and so forth.</p>