2

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..

7
  • 1
    your id selector seems like it might be incorrect...you're putting the value of the textfield where the ID should be. If you fix that it looks like your code should work okay...although it's not the way i'd probably do it to be clear...I think it should be document.getElementById(my_text) Commented Sep 9, 2014 at 18:49
  • Can you provide a piece of HTML you use with your function? Commented Sep 9, 2014 at 18:50
  • 1
    Yes, the id selector is wrong; use getElementById("my_text") --and later, use input.value to actually pull the text. (On another issue, it is generally not wise to use something like "input" for a JavaScript variable name, when in the overall web page, it is an HTML keyword. Some minor variant, like "inpt" can be useful enough without potentially confusing the browser.) Commented Sep 9, 2014 at 18:56
  • It's still not getting the text I have entered it's just getting the last button i pressed which is " " which is showing up null and causing it to error out.. :( Commented Sep 9, 2014 at 19:00
  • 1
    Sorry for not saying this sooner. I didn't because that first Answer below indicates using ".value" differently from the way you did in your original use of the getElementByID() function. If you copied that Answer, your "no method" error should go away. If you didn't copy it, but only did what I suggested in my first comment above, then you should, everywhere you specify the "input" variable, specify "input.value" to reference the string. That includes the line where you are calling "trim()" and getting an error. Commented Sep 9, 2014 at 21:20

2 Answers 2

3

Did you mean

var input = document.getElementById("my_text").value;
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

<textarea id="MyText" rows="10" cols="50" onkeyup="getLastWord(event);"></textarea>


var Old = "";
var New = ""; 
function getLastWord(e)
{
    New = document.getElementById("MyText").value;
    if(e.keyCode == 32)
    {
        Old = New.replace(Old, "");
        alert(Old);
        Old = New;
    }
}

DEMO

1 Comment

This does not accomplish what the OP has asked

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.