[thelist] Using Javascript to add tags to selected text in textarea

Chris Blessing webguy at mail.rit.edu
Thu May 23 13:08:01 CDT 2002


Hi Simon-

I don't know that you'll be able to do this with Mozilla, but it is
certainly possible with IE.  Check this out:

theSelection = document.selection.createRange().text;
document.selection.createRange().text = "*" + theSelection + "*";

This is straight off the phpBB forum software, at phpBB.com.  HTH!

Chris Blessing
webguy at mail.rit.edu
http://www.330i.net

> I'm currently enhancing a Wiki script (Phiki Wiki from
> http://ontosys.com/phiki/PhikiWiki ) and I want to add a feature where by
> users can highlight some text in a text area on the "Edit Page"
> page and click
> a button to add Wiki bold tags (or italic tags or whatever) around the
> selected text. I know how to grab the currently selected text using DHTML:
>
> http://www.xs4all.nl/~ppk/js/selected.html
>
> My problem is then adding content (in this case Wiki tags) to
> either side of
> the selected text. I've thought about doing it by running a simple replace
> operation on the text in the textarea replacing "selectedtext" with
> "*selectedtext*" - however this is not ideal as it will not work if the
> selected text occurs more than once in the textarea (with the
> sentence "the
> cat sat on the mat" for example, selecting the word "the" and hitting the
> button would either add tags to every instance of "the" in the sentence or
> only add tags to the first occurence of the word).
>
> Is it possible to do what I am trying to do with Javascript?
> Ideally I would
> like a solution for both Mozilla and IE, but I don't mind if it
> just works on
> Mozilla.
>
> Cheers,
>
> Simon Willison




More information about the thelist mailing list