By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,398 Members | 1,362 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,398 IT Pros & Developers. It's quick & easy.

Changing the color of individual words in a textarea

P: n/a
Hello,

I'd like to put, on a web page, a "place" where the user can type a
rather long text, with automatic coloring of each word (e.g. a color
depending on the number of letters of the word).

The only solution I could think of is rather ugly: a <textareafor
the user to type in, and behind the <textarea>, one <spanfor each
word, with the same text and font, and a colored background.
I've put it there: <http://perso.edulang.com/temp/demo.html>. (For
this demo, the color changes according to whether the number of
letters is even or odd. On this page, the background color changes,
but I'd prefer if the text color would change.)

This solution doesn't really work well: not only did I have a hard
time handling spaces (two consecutive spaces show in a <textarea>, but
are counted as only one in a regular <span>), but also, there are lots
of cases to handle (the scroller for example).

Is there a good and working solution?
Shall I program my own "extented textarea"?
Or maybe Flash would be better in that case?

Thanks a lot in advance...

Jul 10 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Fabien LE LEZ said the following on 7/10/2006 2:03 PM:
Hello,

I'd like to put, on a web page, a "place" where the user can type a
rather long text, with automatic coloring of each word (e.g. a color
depending on the number of letters of the word).
OK.
The only solution I could think of is rather ugly: a <textareafor
the user to type in, and behind the <textarea>, one <spanfor each
word, with the same text and font, and a colored background.
An IE solution is rather short and simple. Trying to get it to work,
relatively easy, in other browsers may be more difficult.
I've put it there: <http://perso.edulang.com/temp/demo.html>. (For
this demo, the color changes according to whether the number of
letters is even or odd. On this page, the background color changes,
but I'd prefer if the text color would change.)
You can simply change .color instead of .backgroundColor
This solution doesn't really work well: not only did I have a hard
time handling spaces (two consecutive spaces show in a <textarea>, but
are counted as only one in a regular <span>), but also, there are lots
of cases to handle (the scroller for example).
Very true.
Is there a good and working solution?
Shall I program my own "extented textarea"?
Or maybe Flash would be better in that case?
Flash would definitely be more cross-browser friendly.

This is IE only code and may be easily modified for other browsers but I
am not sure. It has an issue with hard returns (enter key) where it
removes them but other than that it colors the words according to
length. If the length exceeds the color array, it uses the last entry in
the colors array.

It is undocumented. It's the results of about an hour of my afternoon so
if you have questions, ask.

<URL: http://members.aol.com/_ht_a/hikksno...oredWords.html >

It could be modified to work where it changed the colors as you type,
but I found that rather annoying and hard on my eyes.

Enjoy.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Temporarily at: http://members.aol.com/_ht_a/hikksnotathome/cljfaq/
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jul 10 '06 #2

P: n/a
On Mon, 10 Jul 2006 17:20:54 -0400, Randy Webb
<Hi************@aol.com>:
>I've put it there: <http://perso.edulang.com/temp/demo.html>.

You can simply change .color instead of .backgroundColor
Nope. Since Javascript is rather slow, the textarea's text needs to
show (I can't just let it be transparent), so the "background span"'s
text color won't be visible, since it's behind.
><URL: http://members.aol.com/_ht_a/hikksno...oredWords.html >
It could be modified to work where it changed the colors as you type,
but I found that rather annoying and hard on my eyes.
There's a huge flaw: when you insert a space in the middle of the
text, the cursor goes to the far end of the text.
I think this can be corrected quite easily on IE, but not on other
browsers (only IE can give you the position of the cursor).

This definitely ain't a simple problem (Hey, if it was, I wouldn't
have posted it here). And I suppose I'll have to learn Flash.

Jul 10 '06 #3

P: n/a
Fabien LE LEZ said the following on 7/10/2006 5:51 PM:
On Mon, 10 Jul 2006 17:20:54 -0400, Randy Webb
<Hi************@aol.com>:
>>I've put it there: <http://perso.edulang.com/temp/demo.html>.
You can simply change .color instead of .backgroundColor

Nope. Since Javascript is rather slow,
"Javascript is rather slow"? Yours or mine? <g>
the textarea's text needs to show (I can't just let it be
transparent), so the "background span"'s text color won't
be visible, since it's behind.
For some reason, I wasn't thinking about it that way but it makes
perfect sense.
><URL: http://members.aol.com/_ht_a/hikksno...oredWords.html >
It could be modified to work where it changed the colors as you type,
but I found that rather annoying and hard on my eyes.

There's a huge flaw: when you insert a space in the middle of the
text, the cursor goes to the far end of the text.
That's a flaw, and it would take some work to fix it. When the space bar
is pressed, you get the cursor position. Then after the innerHTML is
assigned, you put the cursor back where it is. It does what it does now
because it blanks the div and then replaces it which puts the cursor at
the end.

Maybe later tonight or tomorrow I will tinker with it some more and even
work on fixing the <enterkey flaw in it. Type some text, hit the
<enterkey and keep typing. It removes the hard returns.
I think this can be corrected quite easily on IE, but not on other
browsers (only IE can give you the position of the cursor).
That's not that hard to do, but the entire code is IE only anyway so
"not on other browsers" becomes a moot point :)
This definitely ain't a simple problem (Hey, if it was, I wouldn't
have posted it here).
It's an interesting idea, none the less.
And I suppose I'll have to learn Flash.
It would definitely be easier and more cross browser if written in Flash.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Temporarily at: http://members.aol.com/_ht_a/hikksnotathome/cljfaq/
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jul 10 '06 #4

P: n/a
On Mon, 10 Jul 2006 18:14:17 -0400, Randy Webb
<Hi************@aol.com>:
When the space bar
is pressed, you get the cursor position. Then after the innerHTML is
assigned, you put the cursor back where it is.
I've thought about that all right, but AFAIK, there's no way to do
that on non-IE browsers.

And, I'd rather not do too much browser-specific code -- testing and
debugging on a few versions of IE, FF, Opera, Konqueror, Safari, and a
few other, is too time-consuming.
[Note: I'm rather lucky, since making a Links-compatible code isn't
mandatory ;-) ]

Jul 11 '06 #5

P: n/a
Fabien LE LEZ said the following on 7/11/2006 12:27 PM:
On Mon, 10 Jul 2006 18:14:17 -0400, Randy Webb
<Hi************@aol.com>:
>When the space bar
is pressed, you get the cursor position. Then after the innerHTML is
assigned, you put the cursor back where it is.

I've thought about that all right, but AFAIK, there's no way to do
that on non-IE browsers.

And, I'd rather not do too much browser-specific code -- testing and
debugging on a few versions of IE, FF, Opera, Konqueror, Safari, and a
few other, is too time-consuming.
[Note: I'm rather lucky, since making a Links-compatible code isn't
mandatory ;-) ]
Write it in Flash and then test it in any browser with all of the
different versions of plugins and your testing time becomes comparable.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Temporarily at: http://members.aol.com/_ht_a/hikksnotathome/cljfaq/
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jul 12 '06 #6

P: n/a
I wrote:
>I'd like to put, on a web page, a "place" where the user can type a
rather long text, with automatic coloring of each word (e.g. a color
depending on the number of letters of the word).
Well, that does not seem possible in Javascript -- at least, if I want
it to work on most browsers.
So, I've made it in Flash (now *that* is an annoying piece of
software :-/ )
In case someone is interested, I've put the Flash thingy here:
<http://perso.edulang.com/temp/demo-flash.zip>.

Jul 12 '06 #7

P: n/a
On Tue, 11 Jul 2006 23:17:27 -0400, Randy Webb
<Hi************@aol.com>:
>Write it in Flash and then test it in any browser with all of the
different versions of plugins and your testing time becomes comparable.
Well, of course the final result will have to be tested on lots of
different browsers. That job is to be done even if there's only plain
HTML code. And, it must be done by someone else than the programmer.
But here, there's a huge difference between the two solutions:

- javascript: here, we're using advanced, browser-specific features.
Sure, Mozilla and IE have the feature, and their interfaces are close
enough, but that's still browser-specific code. Especially with the
"get selection" feature.
And what about the non-IE, non-Gecko browsers?
In other words, there's a high probability that the result in each
browser will be different.

- Flash: I'm using a basic and well-documented feature. I'm pretty
confident that Macromedia did what was needed to make it work
everywhere. We're talking about only one editor here, not about the
long-lasting "feature war" between Mozilla and Microsoft.
Note: of course, advanced and/or undocumented features might give
different results in different versions of Flash.

Jul 12 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.