View Full Version : TextRange actual position

12-27-2005, 08:32 PM
Hi y'all!!

Given a TextRange (so in IE environment), is it possible to determine which are its current delimiters into the node?
I mean, using
moveStart(...) or
moveEnd(...) methods I'm able to drag them... but how can I count the number of characters before the textrange selection?

Suppose a client is selecting a text portion: how can I know what's the start-offset without moving it?

In Mozilla environment, I'd solve my problem simply reading the
Range.startOffset attribute. How can I afford something similar with IE?

Thanx in advance!!

12-27-2005, 11:06 PM
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

<html xmlns="http://www.w3.org/1999/xhtml"
xml:lang="en" lang="en">

<title>Web Design</title>
<meta name="author" content="Mr. Smith, I presume" />
<meta name="description" content="web" />

<style type="text/css">
background: buttonface;
border-style: inset;
border-width: 3px;
width: 50%;
height: 100px;
overflow: auto;
url(<a href="http://www.grsites.com/folders/my_backgrounds/misc180.gif" target="_blank">http://www.grsites.com/folders/my_b...nds/misc180.gif</a>);
display: block;
margin-top: 25px;
margin-bottom: 0;


<script language="javascript">
<!-- Hide from browsers lacking javascript
function findCursor(){

var oCursor=document.selection.createRange();
window.onload = initialize;

function initialize(){
//Assign a functon to the button's onclick event handler:
document.onclick = get_range;

function get_range(){
var ta_2 = document.f.ta2; //shorten the reference to ta2

if(document.selection && document.selection.createRange){//then IE like broswers
var oRange = document.selection.createRange();//creates a TextRange object encompassing the selected text.

ta_2.value = oRange.text; //puts the selection into the textarea ta_2
else if(window.getSelection){//then NN like browsers
var sText=window.getSelection();
ta_2.value = sText;
else {
alert("example won't work on your browser");

// End hiding -->

<form name="f" method="post" action="">
<div name="ta1" class="box_style" onfocus="findCursor()" >Select some text in this box.</div>
<input type="button" id="b1" name="b1" class="buttons" value="Display selected text"/>
<textarea name="ta2" class="box_style" onfocus="findCursor()" ></textarea>


12-28-2005, 09:22 AM
Thank you for you post vwphillips! But this script shows me which is the selected text, while I'd like to know which is the current position (for example, if my cursor/caret is on the 5th character, I'd like to know how to display "5")... is there a way to do this?

Thanx again