insert text in textarea on right click in java script

Discussion in 'Javascript' started by Max, Oct 14, 2003.

  1. Max

    Max Guest

    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.
    Max, Oct 14, 2003
    #1
    1. Advertising

  2. "Max" <> wrote in message
    news:...
    > 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
    Vjekoslav Begovic, Oct 14, 2003
    #2
    1. Advertising

  3. Max

    max zen Guest

    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!
    max zen, Oct 14, 2003
    #3
  4. "max zen" <> wrote in message
    news:3f8bf3bf$0$202$...
    > thanks for the reply, but it didn't work in either IE or mozilla.


    It works when you right click in textarea.
    Vjekoslav Begovic, Oct 14, 2003
    #4
  5. Max

    Max Guest

    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.
    Max, Oct 15, 2003
    #5
  6. "Max" <> wrote in message
    news:...
    > 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
    Vjekoslav Begovic, Oct 17, 2003
    #6
  7. Max

    max zen Guest

    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!
    max zen, Oct 31, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rick Csucsai

    how to insert text into middle of textarea

    Rick Csucsai, Jun 21, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    266
    Harolds
    Jun 24, 2005
  2. Gordan

    insert text in textarea

    Gordan, Sep 2, 2003, in forum: Javascript
    Replies:
    0
    Views:
    93
    Gordan
    Sep 2, 2003
  3. Lloyd
    Replies:
    2
    Views:
    103
    Lloyd
    Sep 21, 2004
  4. Leon
    Replies:
    3
    Views:
    170
    TaeHo Yoo
    Nov 26, 2004
  5. mariomueller
    Replies:
    2
    Views:
    551
    mariomueller
    Aug 24, 2006
Loading...

Share This Page