View Full Version : Formatting text in a text box ? (bold/italic/underline etc)

05-01-2006, 11:02 AM
Ive looked around with limited success, just wondering if someone could direct me to a tutorial, give some advice or give me some sample code on how one would go about formatting text in a text box.

For example, i need to be able to format 'selected text' in a text box to make it bold/italic etc.

Thanks in advance.

05-01-2006, 11:31 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"


<script language="JavaScript" type="text/javascript">

function CngFont(obj,prop,assign){

<input value="Some Text" style="font-weight:bold;" onclick="CngFont(this,'fontStyle','italic');" >


05-02-2006, 12:24 AM

If I had a textbox, is it possible to apply the formatting to a selected text ?
i.e. select certain text, click eg italic.


05-02-2006, 11:12 AM
Comment Retracted

05-02-2006, 12:22 PM
Not really. First at all, there are at least 3 ways to capture the selection, according to the browser's type

window.getSelection() // Moz, Safari
document.getSelection// Moz, IE 5 for MAC
document.selection.createRange().text// IE 5+ for Win

(well, the object which supports the methods might be the text box, at least in IE, not the document or the window - not test in the rest)

But the main problem is, if I understood well, that khorask wants to "select" a part of the text which is a text box and change it's style, which , as far as I know - I might be wrong - but I think it is impossible, as you may change only the style of the whole text in text box, but you may not isolate a part of it (as you may not insert a <span> object inside the box's value).

09-04-2007, 10:13 PM
This is old but I will add to the thread.

You cannot do this with the textbox. Rather you can use javascript to replace the textbox/area with an editable iframe and add more js to handle the events.

I would suggest using something like www.wymeditor.org or open wysiwyg from openwebware.com