tags:

views:

351

answers:

4

i am doing spell check application. i want to change the color of text after enter space. i got that wrong word. now i want how to change color of that text in textbox. using javascript.

+3  A: 

Javascript can modify the CSS very easily, please check my example:

<input type="text" value="123" id="text1"/>
<script type="text/javascript">
document.getElementById("text1").style.color = "green";
</script>

Cheers,

Andrew

REA_ANDREW
A better solution would be to have a separate CSS class for incorrectly spelled words and to switch the CSS class when needed. +1 because the solution provided is in the right direction.
Cerebrus
+1  A: 

I don't think you can change the colour of just a word in a textarea.

But if you want to change the colour of text just in a text input, then the method outlined by rea_andrew should suffice. Just do a

document.getElementById("textboxid").style.color = "green";
trex279
+1 for bolding "textarea". It was (very) easy to miss in the OP's question.
Cerebrus
+2  A: 

Or use some of the libraries like JQuery. Using JQuery, it's simple.

Having

<input name="my-text" type="text" id="my-text">

Catch the key down event

$("input#my-text").keydown(function(event)
{
   switch (event.keyCode) {
      // space
      case 32: $(this).css("color","#f00"); //or set any other property
         break;
   }
}

If you want to highlight single word, you'll need to wrap it into span and change property just for this element.

And yes, to highlight a sigle word you need to use paragraph.

Igor Pavelek
A: 

You'd basically need to do the same kind of thing TinyMCE and friends do building richer editors on top of the textarea. You are far better off using a pre-made library to do this for you. It is a lot more complicated than just changing the color, because you need to build a whole editor underneath that.

ironfroggy