By the way...
This could be pretty slow if there's a lot of data between the DIVs...
Dave
Main Topics
Browse All TopicsImagine the following scenario:
A user highlights a part of a paragraph and presses a button that runs a piece of javascript. The javascript returns the start and end points of the selected text, in relation to the div it is in. So for example, if I selected the W from the following line, the script would return the value start=7 and end=8:
<div>Hello World</div>
The catch is, if the div contains additional code within it, the start and end values should remain the same, so the values should still be start=7 and end=8, even when the user highlights the W of the following line:
<div>Hell<a href=#>o</a> World</div>
In IE, I can acheive this by using the following code snippet:
var element = document.getElementById( 'thediv' );
if( document.selection ){
var range = document.selection.createR
var stored_range = range.duplicate();
stored_range.moveToElement
stored_range.setEndPoint( 'EndToEnd', range );
element.selectionStart = stored_range.text.length - range.text.length;
element.selectionEnd = element.selectionStart + range.text.length;
}
element.selectionStart/End
How would I achieve the same using FireFox?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Thank you for your suggestion. I tried it, and you were right, it is very slow on large DIVs, and unfortunately, my DIVs are approx 50.000 characters each. Also, the script looks for the first instance of the selection, so if the selection contents is found several places in the DIV, we're in trouble.
I've spent the entire day looking for a solution, the only pure javascript solution I can think of would be something like this (in quasi code)
Find selection start point within the current node.
Add the length of the current node to its parent node.
Repeat till you're at root (main div) level.
If someone knows the key lines and can share them I would be more appreciative. If not, I will have to settle for a hybrid version of the above, that includes PHP (the PHP will do exactly the same as the above, but faster). The benefit of using PHP is (in addition to the increased speed) that if there are several instances of the selected text, let the user decide which one they've chosen. I would be very happy if I could avoid this long-winded approach.
There must be some way for Firefox to do what IE can do in like 6 lines.
Business Accounts
Answer for Membership
by: nbkbar7Posted on 2007-01-16 at 09:57:10ID: 18325978
get your selection using document.getSelection,
vName);
ype == 1) { // 1 = element ype == 3) { // 3 = text lue;
loop thru the div looking for text elements to assemble the displayed string,
get the indexOf the selection within the DIV's string
give this a try...
lem'me know if you have questions or it dosen't work right...
Dave
<html>
<head>
<script>
function getSel(divName)
{
var txt = '';
var foundIn = '';
var num = 0;
var obj = document.getElementById(di
var divStr = '';
var begin = -1;
var end = -1;
if (document.getSelection) { // tells us we're in FF
txt = document.getSelection();
divStr = getDIVString(obj);
begin = divStr.indexOf(txt);
if (begin > -1) {
begin = begin + 1;
end = begin + txt.length;
} else {
// not found
}
}
alert ("selection -> " + txt + "\ndivStr -> " + divStr + "\nbeg [" + begin + "]\nend [" + end + "]");
}
function getDIVString (obj) {
var divStr = '';
for (ndx = 0; ndx < obj.childNodes.length; ndx++) {
if (obj.childNodes[ndx].nodeT
divStr = divStr + getDIVString (obj.childNodes[ndx]);
} else {
if (obj.childNodes[ndx].nodeT
divStr = divStr + obj.childNodes[ndx].nodeVa
}
}
}
return divStr;
}
</script>
</head>
<body>
<div id="thediv">This <a href="#NAME">is</a> some text</div>