[thelist] Using Javascript to add tags to selected text in te xtarea

Scott Schrantz scotts at rci-nv.com
Fri Aug 29 11:09:19 CDT 2003


> -----Original Message-----
> From: Pietro Di Bello [mailto:P.DiBello at quinary.com]
> 
> I read this thread by Simon Willison, it was posted one year 
> ago...And, since I have the same problem, I was wandering if 
> anyone has been able to handle this issue in the meanwhile...

The code I'm using works with Mozilla. I got it from Blogger, which in turn claims to have gotten it from massless.org.

function mozWrap(txtarea, lft, rgt) {
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	if (selEnd==1 || selEnd==2) selEnd=selLength;
	var s1 = (txtarea.value).substring(0,selStart);
	var s2 = (txtarea.value).substring(selStart, selEnd)
	var s3 = (txtarea.value).substring(selEnd, selLength);
	txtarea.value = s1 + lft + s2 + rgt + s3;
	txtarea.focus();
}

Then you call it in an onclick attribute.

<button onclick="mozWrap(postform.posttext,'<em>','</em>');
return false;">

There's another function that works for IE, and a third function that does browser detection and calls the proper function for the browser. You can see it all at
www.computer-vet.com/blogomatic/
www.computer-vet.com/blogomatic/blog_shortcuts.js

--
Scott Schrantz
work: www.rci-nv.com
play: www.computer-vet.com/weblog/


More information about the thelist mailing list