views:

172

answers:

4

I have a regular texbox control. I need to highlight some words with a red color. Is it any possible to do with JavaScript (jquery or anything else)?

A: 

No, you can't do that. Your only way would be to use a rich text editor component like FCKEditor or similar.

RichieHindle
Well, technically you can as all those controls do is to use Javascript on a TextArea element and so forth.
BobbyShaftoe
rubbish. they are richtext controls. this is a completely different beast to a textarea.
SpliFF
@SpliFF, so how does TinyMCE work then? Have a look at the example here http://tinymce.moxiecode.com/examples/full.php. It's just a textarea control and a big pile of java script.
Glen
Tinymce inserts an iframe dynamically and hides the textarea. Use firebug/firefox to see the actual content of a the example and search for 'content_ifr'
KooiInc
SpliFF is 100% correct: A textbox cannot contain child elements ergo it cannot style individual substrings. End of.
annakata
thanks for the clarification guys. Makes sense now.
Glen
+5  A: 

Most rich text javascript editors use an iframe with designMode='on' since that yields the best cross browser results:

<iframe ID="rtbox"></iframe>

To make the iframe editable and insert rich text via Javascript you can use the following sample code:

var rtbox = document.getElementById('rtbox');
var doc = rtbox.document ? rtbox.document : rtbox.contentDocument;
doc.designMode = 'on';
doc.body.innerHTML = 'one <span style="color:red">red</span> word';
Josef
A: 

No you cannot use different styles in a standard <textarea>.

DrG
A: 

I recommend using TinyMCE for a rich text editor.

And no, what you say is not possible on a normat textarea.

Here Be Wolves