"Max" <maximuszen@optonline.net> wrote in message
news:3a9c1232.0310151029.4e51c980@posting.google.c om...[color=blue]
> yes, you're right it does work. i don't know what happened the first
> time. the exact function that i am looking for is to insert a "*"
> whenever i click in the textarea that already contains text. the
> posted function works but only adds the predefined text. i want to
> insert into the cursor location the pre defined text. i tried += but
> it only appends the predefined text. I will need to insert the
> predefined text multiple number of times at different cursor
> locations.[/color]
OK, this works on IE6 (not tested on other browsers).
<script type="text/javascript">
// I found this on
http://www.faqts.com/knowledge_base/...d/1052/fid/130
function storeCaret (textEl) {
if (textEl.createTextRange)
textEl.caretPos = document.selection.createRange().duplicate();
}
function insertAtCaret (textEl, text) {
if (textEl.createTextRange && textEl.caretPos) {
var caretPos = textEl.caretPos;
caretPos.text =
caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?
text + ' ' : text;
}
else
textEl.value = text;
}
</script>
<script type="text/javascript">
var myText = "Predefined text";
function insertText(obj,e){
var button = e.button || e.which;
if (button == 2){
insertAtCaret(obj,myText)
}
}
</script>
<textarea onmousedown="storeCaret(this);insertText(this,even t);"></textarea>
HTH,
Vjekoslav