View Full Version : Selected Text

05-01-2006, 11:19 PM
Im trying to make a simple txt/html editor...

I need to apply specific formatting to selected text via a button.
for example, I select some text, click the 'bold' button which then replace the selected text with <b>selected text</b>

Is there a way to do this ?

05-02-2006, 01:22 PM
For IE is rather simple:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function getSel(tag,endtag){
var txt='';
txt = document.selection.createRange().text
document.selection.createRange().text=txt.replace(new RegExp(txt),tag+txt+endtag)
<input name="bold" type="button" value=" B " style="font-weight:bold"onclick="getSel('<strong>','</strong>')"> Bold <input name="italic" type="button" value=" I " style="font-weight:bold;font-style:italic"onclick="getSel('<em>','</em>')"> Italic<br><br>
<textarea cols="50" rows="10">Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</textarea>

For Moz is quite intricate. I'll have to test later some DOM methods to try to find an equivalent (does anyone has something like that?)