471,310 Members | 1,085 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,310 software developers and data experts.

insert text in textarea on right click in java script

Max
i want to insert a predefined string in a textarea when i right click
on the mouse. i need to do it in internet explorer and mozilla.
Jul 20 '05 #1
6 11462
"Max" <ma********@optonline.net> wrote in message
news:3a*************************@posting.google.co m...
i want to insert a predefined string in a textarea when i right click
on the mouse. i need to do it in internet explorer and mozilla.


Try:

<script type="text/javascript">
var myText = "Predefined text";
function insertText(obj,e){
var button = e.button || e.which;
if (button == 2){
obj.value=myText; // or obj.value += myText;
}
}
</script>
<textarea onmousedown="insertText(this,event);"></textarea>

Vjekoslav
Jul 20 '05 #2
thanks for the reply, but it didn't work in either IE or mozilla.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #3
"max zen" <ma********@optonline.net> wrote in message
news:3f*********************@news.frii.net...
thanks for the reply, but it didn't work in either IE or mozilla.


It works when you right click in textarea.
Jul 20 '05 #4
Max
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.
Jul 20 '05 #5
"Max" <ma********@optonline.net> wrote in message
news:3a**************************@posting.google.c om...
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.


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
Jul 20 '05 #6
The code does work, but incorrectly. It inserts the predefined text into
the previous cursor position. So there is a single click delay. The
predefined text does not appear immediately. On the subsequent click,
the predefined text is inserted into the previous cursor position. Also
we have the context menu pop up also. We lay this away for a while and
have been working on it without the desired solution. I would appreciate
your attention.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Doug van Vianen | last post: by
15 posts views Thread by globalrev | last post: by
reply views Thread by brianrpsgt1 | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.