Solved

getSelection Start/End position

Posted on 2006-07-06
2
924 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
2 Comments
 
LVL 28

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now