PDA

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



Iiro
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?

jscheuer1
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)

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

Iiro
03-20-2006, 02:49 PM
thanks!

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

Twey
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.focus();
el.selectionStart = begin.length;
el.selectionEnd = (begin + opening + middle + closing).length;
el.scrollTop = scroll;
} else {
el.value += opening + closing;
el.focus();
}
}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...