471,350 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

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

Jul 23 '05 #1
2 2117
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.

Jul 23 '05 #2


ma**********@gmail.com wrote:
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>
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Srinath Avadhanula | last post: by
4 posts views Thread by G Uljee | last post: by
13 posts views Thread by André Nogueira | last post: by
2 posts views Thread by naima.mans | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.