Is there any Javascript function or method that returns selected text?

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

    Is there any Javascript function or method that returns selected text?

    For e.g, I have a textarea and I want to get only the selected text
    (the text I mark with the mouse).
    Anybody?

    ThX
  • Olexiy Merenkov

    #2
    Re: Is there any Javascript function or method that returns selected text?

    Hello,

    function getSel()
    {
    if (document.getSe lection) txt = document.getSel ection();
    else if (document.selec tion) txt = document.select ion.createRange ().text;
    else return;

    document.forms[0].selectedtext.v alue = txt.replace(new
    RegExp('([\\f\\n\\r\\t\\v ])+', 'g')," ");
    }
    I've found this function here:http://www.xs4all.nl/~ppk/js/selected.html
    Kindest Regards,
    Olexiy Merenkov

    [color=blue]
    > For e.g, I have a textarea and I want to get only the selected text
    > (the text I mark with the mouse).
    > Anybody?
    >
    > ThX[/color]


    Comment

    • DU

      #3
      Re: Is there any Javascript function or method that returns selectedtext?

      SpArKy wrote:[color=blue]
      > For e.g, I have a textarea and I want to get only the selected text
      > (the text I mark with the mouse).
      > Anybody?
      >
      > ThX[/color]

      function ShowSelectedTex t()
      {
      if (window.getSele ction) // recent Mozilla versions
      {
      var selectedString = window.getSelec tion();
      }
      else if (document.all) // MSIE 4+
      {
      var selectedString = document.select ion.createRange ().text;
      }
      else if (document.getSe lection) //older Mozilla versions
      {
      var selectedString = document.getSel ection();
      };

      alert("The text you selected is \n'" + selectedString + "'");
      }

      DU
      --
      Javascript and Browser bugs:

      - Resources, help and tips for Netscape 7.x users and Composer
      - Interactive demos on Popup windows, music (audio/midi) in Netscape 7.x


      Comment

      Working...