0

I was wondering if there are any JavaScript functions that I could use to change the font color of certain strings in a text box.

Suppose I created a text box and every time the string hello appeared the font color would change to blue.

Are there any easy ways to make a database of strings so that this event would occur?

thanks!

user1472145
  • 23
  • 1
  • 4
  • 2
    Do you mean Java, or JavaScript (for which "JS" is a common abbreviation)? They are not remotely the same thing. – Matt Ball Jun 21 '12 at 12:57
  • What have you tried? Perhaps look at http://stackoverflow.com/questions/5657092/change-color-of-specific-string to see if it answers your question – radimpe Jun 21 '12 at 12:58

1 Answers1

1

This is a non-trivial task, as text within a textarea can not be directly formatted. I would look into using a div or some other applicable tag, and use the Content Editable attribute. This way the user can edit the text and you can control the formatting. At the simplest level you can listen for a key press and use regular expressions or the replace method to highlight all the words in your dictionary.

Here's a start, you'll need to flesh it out to perhaps be case-insensitive if that's what you want, and to keep track of the caret position which is a more complicated task:

http://jsfiddle.net/VJQHD/

You can look at a similar issue here: Get caret (cursor) position in contentEditable area containing HTML content

Community
  • 1
  • 1
Cecchi
  • 1,525
  • 9
  • 9