View Full Version : "Wrap" tags around selected text?!?!?

03-19-2006, 04:38 PM
Hi, I'm building new guestbook for my site. Is there any way to "wrap" text around selected text? For example:
I have small textarea.
Visitor writes words "Hello World" in textarea and selects it.
Now visitor pushes button and value of textarea is now "<b>Hello World</b>".

Is there any way to do that?

03-19-2006, 07:12 PM
Have a look at:

Dynamic Drive- Rich Text Editor (for textareas) (http://www.dynamicdrive.com/dynamicindex16/richtexteditor/index.htm)

03-19-2006, 08:03 PM
There are easier ways... modify this (http://phpxperts.com/hasin/?p=139).

03-20-2006, 02:49 PM

03-20-2006, 02:53 PM
but how can I add text to other side the selection?

03-20-2006, 04:39 PM
I did say to modify it.

function wrapWith(fieldId, opening, closing) {
var el = document.getElementById(fieldId);
if(document.selection) {
// I'm not sure how this would be done with IE, after looking at MSDN.
// Maybe someone else knows.
} else if(el.selectionStart || el.selectionStart == 0) {
var begin = el.value.substring(0, el.selectionStart),
end = el.value.substring(el.selectionStart + 1, el.selectionEnd),
middle = el.value.substring(el.selectionEnd + 1),
scroll = el.scrollTop;
el.value = begin + opening + middle + closing + end;
el.selectionStart = begin.length;
el.selectionEnd = (begin + opening + middle + closing).length;
el.scrollTop = scroll;
} else {
el.value += opening + closing;
}May need tweaking. Definitely needs IE support. MSDN is annoyingly cryptic, and Microsoft seem to have set out to build one of the most useless APIs they possibly could for this purpose...