Select DOM node(s) via text highlighting

M

mail.goodies

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.getSelection() 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.getSelection() with the Range API.

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

mail.goodies

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

Martin Honnen

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.

You need to use
var selection = window.getSelection();
which gives you a selection object with the properties and methods
defined here:
<http://lxr.mozilla.org/seamonkey/source/content/base/public/nsISelection.idl#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.getRangeAt(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>
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top