By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,747 Members | 1,672 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,747 IT Pros & Developers. It's quick & easy.

insert text in textarea on right click in java script

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
"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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
"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

P: n/a
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.