Selecting text on-the-fly from textbox leaves out last character in javascript
<p>I'm trying to get the text from a textbox as a user types, so that I can parse it and display information accordingly as the user enters a command. However, it seems as though the function I'm writing is getting the text from the box before the letter is entered into the text box. How do I prevent the function from grabbing the content from the textbox before the typed character is entered? I considered grabbing the id of the key and altering the inputted string accordingly, but I feel like there should be a better way. </p>

<p>The code: </p>

<pre><code>$('#inputConsoleForm').keydown(function(event){
    //Get key code
    var code = (event.keyCode ? event.keyCode : event.which);
    //Get console text (doesn't behave as expected)
    var consoleCommand = document.inputConsoleForm.console.value;

    function parseConsoleCommand(consoleCommand) {
        /* Returns true if command is valid*/
    }
    if(code === 13) {
        event.preventDefault();
        if(!parseConsoleCommand(consoleCommand))
            alert("INVALID COMMAND LINE");
        else
            attemptExecute();//Runs the command
    }
    if(code === 32 || (code &gt;= 48 &amp;&amp; code &lt;= 123) || code === 61 || code === 109 || code === 188 || code === 8) {
        if(parseConsoleCommand(consoleCommand)){
            $(document.inputConsoleForm.console).css("background-color", "#FFDFDF");
        }
        else{
            $(document.inputConsoleForm.console).css("background-color", "");
        }
    }
});
</code></pre>
