Select DOM node(s) via text highlighting

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mail.goodies@gmail.com

    #1

    Select DOM node(s) via text highlighting

    Hello,

    I am new to javascript.
    I want to be able to do the following if possible:

    1) Use the mouse to highlight a section of text on a page.
    2) Obtain references (via javascript) to the node(s) that contain the
    highlighted text.
    3) Manipulate the html of these node(s).

    I understand there is a method document.getSel ection() that returns
    the highlighted text. However, I am unable to obtain the container
    node(s) of the text. I have also found the javascript Range API, but
    I am unsure how I can interface the highlighted text from
    document.getSel ection() with the Range API.

    Does anyone have any advice or suggestions on how to accomplish this?
    Thanks,
    Ralph

  • mail.goodies@gmail.com

    #2
    Re: Select DOM node(s) via text highlighting

    Some additional information (sorry for leaving it out):
    I want to do this in mozilla firefox 1.0 Gecko/20041001
    This is for an intranet application.

    Comment

    • Martin Honnen

      #3
      Re: Select DOM node(s) via text highlighting



      mail.goodies@gm ail.com wrote:
      [color=blue]
      > Some additional information (sorry for leaving it out):
      > I want to do this in mozilla firefox 1.0 Gecko/20041001
      > This is for an intranet application.[/color]

      You need to use
      var selection = window.getSelec tion();
      which gives you a selection object with the properties and methods
      defined here:
      <http://lxr.mozilla.org/seamonkey/source/content/base/public/nsISelection.id l#54>
      so basically if a selection has content then rangeCount is 1 (or
      greater) and you can get at a range using
      var range = selection.getRa ngeAt(0);
      and that range implements the properties and methods documented here:
      <http://lxr.mozilla.org/seamonkey/source/dom/public/idl/range/nsIDOMRange.idl #52>
      following the W3C DOM Level 2 Range specification:
      <http://www.w3.org/TR/DOM-Level-2-Traversal-Range/ranges.html>


      --

      Martin Honnen
      http://JavaScript.FAQTs.com/

      Comment

      Working...