• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 985
  • Last Modified:

getSelection Start/End position

Hi there, been searching the web with no avail.  Basically, what I'm trying to do is get the start/end position of highlighted text.  So, say for example we have:

Hello my name is Jimmy

The word 'Hello' is highlighted, button clicked.  Start position would be 0, end 4 or so.

Positions are pretty much all I need to know, the selection would initially be made with getSelection() or createRange() for compatibility.

I've seen some methods such as anchorOffset, focusOffset, anchorNode, anchorOffset, but I just can't seem to get them working.  JS newbie, the DOM documentation and web documentation isn't really helping.  

Any thoughts would be highly appreciated.
0
jamesktaylor
Asked:
jamesktaylor
1 Solution
 
Pravin AsarPrincipal Systems EngineerCommented:
<html>
<head>
<title>Selected Text Position</title>
<script language="javascript">
function searchword(evt) {
     if (!evt) { evt = window.event; }
     var srcText = null;
     if (navigator.appName!='Microsoft Internet Explorer') {
                     var t = document.getSelection();
          srcText = evt.target.innerHTML;
          findPos(srcText, t);
     }
     else {
          srcText = evt.srcElement.innerHTML;
          var t = document.selection.createRange();
          if(document.selection.type == 'Text' && t.text>'') {
               document.selection.empty();
               findPos(srcText, t.text);
          }
     }  
}
function findPos(srcText, text) {
      var spos = srcText.indexOf (text);
      var epos = spos + text.length -1;
        window.status = 'Start Position ' + spos + ' End Position ' + epos;
        alert ('Start Position ' + spos + '\n End Position ' + epos);
}
function od(t) {
     while (t.substr(t.length-1,1)==' ')
          t=t.substr(0,t.length-1);
     while (t.substr(0,1)==' ')
          t=t.substr(1);
     if (t) window.location.href = 'http://www.google.com/search?hl=en&q='+escape(t);
}
</SCRIPT>
</head>
<body onDblClick="searchword(event);">
<span>Hello this is a Javascript function to get the start and end position of highlighted text on a page</span> <br>Double click to get information about the start and end position. Watch the status bar.


</body>
</html>
0
 
jamesktaylorAuthor Commented:
This looks to be pretty close to what I'm looking for, however I can't seem to get this to work on a non-double click.  With an href or a button, it's killing the selected text and... Well, making people double click nothing doesn't work.  Any ideas why a button/href fail on this?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now