473,399 Members | 3,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Changing the color of individual words in a textarea

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
7 3821
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: janet | last post by:
how can i count how many words have i written in a text area??? Like taking an example ... i am writing in this textarea of microsoft usergroup. and say in total i have written 50 words .. how...
1
by: John D | last post by:
I am trying to change the background colour of either a div or a textarea with javascript, but am having problems. I have passed a hex colour value in the variable hval, and with the following...
5
by: martin | last post by:
I needed a way to display calculated, multiple, changing values (numerical sums) as users interacted with the page, and do this without going back to the server to load the page again. What I...
5
by: aqualizard | last post by:
I have searched and searched and searched... Can someone please tell me how (or "if" it is even possible) to change selected text in a textarea? Specifically, say a user has highlighted "ppl"...
12
by: dan.vendel | last post by:
Hi, I know nothing about javascript, but quite a lot about regulat html and CSS. Have bumped into a problem that people in this fine congregation perhaps can help me with. I'm making a...
16
by: StenKoll | last post by:
Help needed in order to create a register of stocks in a company. In accordance with local laws I need to give each individual share a number. I have accomplished this by establishing three tables...
14
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only...
2
by: woocha | last post by:
I read this thread and realized that it was exactly what I was looking for. The problem is I need it to be done with sentences not words. I have a problems with typing the same thought down twice...
2
by: sejal17 | last post by:
hello I want to check duplicate word in textarea.In textarea,all words are separated by comma.No words are allowed with enter or white space.And the limit is only 7 words i want to insert.I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.