0

I have a Input box where I need to set the color of text which is being entered on the fly. I was not able to set the color using the content.style.color property. Here is the fiddle for highlighting word in input box

2 Answers 2

4

Change content.style.color = 'red';

to text.style.color = 'red';

You want to set the style on the element, not the element's value.

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

Comments

0

Hello!

content was not defined.

this should work for you

  var text = document.getElementById('text');
        text.addEventListener("keyup", function(event){
          func(event)});
          console.log(text);

        var colors = ["red","green","blue","pink","maroon"];

        var i = 0;

        function func(e){
          if(!(e.keyCode==32)){
            var content = text.value;
            document.getElementById('text').style.color = 'red';
          }

        }
<div class="highlight">
      <input type="text" name="" value="" id="text">
    </div>


Hope I helped you!

3 Comments

Well, you coded it that way, you wrote that if keyCode != 32 the text color should change. I thought that you intended it
You mean OP coded it that way. I was stupid and didn't read the rest of the code. At least I'll admit it.
Oh yea, sorry, It's late haha ^^

Your Answer

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