insert text in textarea on right click in java script

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Max

    insert text in textarea on right click in java script

    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.
  • Vjekoslav Begovic

    #2
    Re: insert text in textarea on right click in java script

    "Max" <maximuszen@opt online.net> wrote in message
    news:3a9c1232.0 310132230.1680b f4@posting.goog le.com...[color=blue]
    > 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.[/color]

    Try:

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



    Vjekoslav


    Comment

    • max zen

      #3
      Re: insert text in textarea on right click in java script

      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!

      Comment

      • Vjekoslav Begovic

        #4
        Re: insert text in textarea on right click in java script

        "max zen" <maximuszen@opt online.net> wrote in message
        news:3f8bf3bf$0 $202$75868355@n ews.frii.net...[color=blue]
        > thanks for the reply, but it didn't work in either IE or mozilla.[/color]

        It works when you right click in textarea.


        Comment

        • Max

          #5
          Re: insert text in textarea on right click in java script

          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.

          Comment

          • Vjekoslav Begovic

            #6
            Re: insert text in textarea on right click in java script

            "Max" <maximuszen@opt online.net> wrote in message
            news:3a9c1232.0 310151029.4e51c 980@posting.goo gle.com...[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

            function storeCaret (textEl) {
            if (textEl.createT extRange)
            textEl.caretPos = document.select ion.createRange ().duplicate();
            }
            function insertAtCaret (textEl, text) {
            if (textEl.createT extRange && textEl.caretPos ) {
            var caretPos = textEl.caretPos ;
            caretPos.text =
            caretPos.text.c harAt(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(o bj,myText)
            }
            }
            </script>
            <textarea onmousedown="st oreCaret(this); insertText(this ,event);"></textarea>

            HTH,

            Vjekoslav


            Comment

            • max zen

              #7
              Re: insert text in textarea on right click in java script

              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!

              Comment

              Working...