Solved

getSelection Start/End position

Posted on 2006-07-06
2
944 Views
Last Modified: 2012-05-07
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
Comment
Question by:jamesktaylor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 29

Accepted Solution

by:
Pravin Asar earned 500 total points
ID: 17049803
<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
 

Author Comment

by:jamesktaylor
ID: 17055111
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

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question