insert text in textarea on right click in java script

M

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.
 
V

Vjekoslav Begovic

Max said:
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
 
M

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.
 
V

Vjekoslav Begovic

Max said:
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/view.phtml/aid/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,event);"></textarea>

HTH,

Vjekoslav
 
M

max zen

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top