I'm wondering how to get the last word typed in Javascript. I have a text area my_text and when the user hits the spacebar it will get the last word that the user typed. This is what I'm trying so far
function getLastWord() {
var input = document.getElementById(my_text.value);
//var lineIn = document.getElementById(my_text).innerHTML;
var lastWordTyped
var changeColorOfWord;
if (input == null) {
input == " ";
}
lastWordTyped = input.substr(input.trim().lastIndexOf(" ") + 1);
When the function gets called on a spacebar hit, it says that input is null so when it gets the lastWordTyped var, it shows up null and errors out, does anybody know why this may be happening?
Preferably no JQuery
Here's some of the HTML to go with it.
<body>
<br />
<!-- Text area -->
<textarea class="text_edit" id="my_text" onkeypress="return myKeyPress(event)" onkeydown="return onKeyDown(event)"></textarea>
<br />
<!-- Submit button -->
<input type="button" value="Run Code" onclick="view_text()" />
<!-- Empty div to put the text in -->
<div id="view_text">
</div>
Ok, so I got the error to go away now i just need it to change the font color of the word typed lol..
" "which is showing up null and causing it to error out.. :(