0

I have a function that I want to change the font color of the user entered string if it is equal to a certain word located in an array.. So far when I step through it it says that it changes the font color but it actually never updates it to the screen and I don't know why. Here is what I have so far

function getLastWord() {
    var input = document.getElementById("my_text");
    //var input = document.getElementById(textArea.value);
    //var lineIn = document.getElementById(my_text).innerHTML;
    var inputValue = input.value;
    var lastWordTyped
    var changeColorOfWord;

    if (input == null) {
        input == " ";
    }

    //lastWordTyped = input.substr(input.trim().lastIndexOf(" ") + 1);
    lastWordTyped = inputValue.substr(inputValue.trim().lastIndexOf(" ") + 1);

    if (input != null) {

        for (var i = 0; i < reservedKeyWords.length; i++) {
            if (reservedKeyWords[i] === lastWordTyped) {
                lastWordTyped = lastWordTyped.fontcolor("blue");
                my_text.replace(inputValue, lastWordTyped);
            } else {

            }


        }
    }
}
1
  • You can't change the color of a text inside a input element, you must use a content editable element. Commented Sep 9, 2014 at 19:36

2 Answers 2

1

I see two issues with the code thus far.

  1. You are using 'fontcolor("blue")' parameter on the lastWordTyped. The proper syntax to change color is element.style.color="#CCC".

  2. You will need to wrap the last typed word in a span so you can target it and apply the color to just that word.

Sign up to request clarification or add additional context in comments.

2 Comments

Actually, fontcolor is a working function even if it's really old. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
How do I wrap it in a span? I got the color working now, but now it makes every word blue lol
1

string.fontcolor is legacy, and should not be used even though I could see it as a viable option in this case

Essentially, what you are doing is adding font tags around the word:

var txt = 'hello world';
txt = txt.fontcolor('blue');
//txt = '<font color="blue">hello world</font>';

You do not show what you do with the result, but if you actually put it in an HTML element it should work, even though instead of using fontcolor, I'd rather use element.style.color. This would require slightly more work though:

var ele = document.querySelector('#my_text');
ele.style.color = 'blue';
ele.innerHTML = lastWordTyped;

If you still want to go with the .fontcolor method, you could just keep what you have in the question and add

input.innerHTML = my_text;

2 Comments

Thank you so much! This worked, although now it makes every word blue, it doesnt stop after checked the last word lol
I think I added the simple solution as the last option

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.