Javascript – Is it possible to change the color of a word in a textbox


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)?

Best Solution

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';