By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,945 Members | 1,540 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,945 IT Pros & Developers. It's quick & easy.

Select DOM node(s) via text highlighting

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a


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.