<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Finding selection start and end position in a textarea, in Internet Explorer</title>
	<atom:link href="http://the-stickman.com/web-development/javascript/finding-selection-cursor-position-in-a-textarea-in-internet-explorer/feed/" rel="self" type="application/rss+xml" />
	<link>http://the-stickman.com/web-development/javascript/finding-selection-cursor-position-in-a-textarea-in-internet-explorer/</link>
	<description>Random developer notes</description>
	<lastBuildDate>Fri, 09 Jul 2010 18:01:55 -0400</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Simon Sanderson</title>
		<link>http://the-stickman.com/web-development/javascript/finding-selection-cursor-position-in-a-textarea-in-internet-explorer/comment-page-1/#comment-1413</link>
		<dc:creator>Simon Sanderson</dc:creator>
		<pubDate>Wed, 09 Jun 2010 05:20:27 +0000</pubDate>
		<guid isPermaLink="false">http://the-stickman.com/uncategorized/finding-selection-cursor-position-in-a-textarea-in-internet-explorer/#comment-1413</guid>
		<description>Hi Jerry - Your solution is perfect ... almost.  Unfortunatley I cannot see why it doesn&#039;t work in this one case.

For IE6: if I have a textarea such as

ABC

and I call your function for every move of the cursor I get the following debug trace in my software:

Selection mode mode (1 char at a time)
Start(0), End(1), Length(1), Text(A)
Start(1), End(2), Length(1), Text(B)
Start(2), End(3), Length(1), Text(C)

Non selection mode (simply pressing right arrow)
Start(0), End(0), Length(0), Text()
Start(1), End(1), Length(0), Text()
Start(2), End(2), Length(0), Text()
Start(1), End(1), Length(0), Text()

as you can possible see from the last line when I the cursor is to the right of the &#039;C&#039; I would expect the output to read
Start(3), End(3), Length(0), Text()

This is what happens in Firefox (albeit through a different branch of the function)

Any ideas how to modify the code to correct this small inconsistency?</description>
		<content:encoded><![CDATA[<p>Hi Jerry &#8211; Your solution is perfect &#8230; almost.  Unfortunatley I cannot see why it doesn&#8217;t work in this one case.</p>
<p>For IE6: if I have a textarea such as</p>
<p>ABC</p>
<p>and I call your function for every move of the cursor I get the following debug trace in my software:</p>
<p>Selection mode mode (1 char at a time)<br />
Start(0), End(1), Length(1), Text(A)<br />
Start(1), End(2), Length(1), Text(B)<br />
Start(2), End(3), Length(1), Text(C)</p>
<p>Non selection mode (simply pressing right arrow)<br />
Start(0), End(0), Length(0), Text()<br />
Start(1), End(1), Length(0), Text()<br />
Start(2), End(2), Length(0), Text()<br />
Start(1), End(1), Length(0), Text()</p>
<p>as you can possible see from the last line when I the cursor is to the right of the &#8216;C&#8217; I would expect the output to read<br />
Start(3), End(3), Length(0), Text()</p>
<p>This is what happens in Firefox (albeit through a different branch of the function)</p>
<p>Any ideas how to modify the code to correct this small inconsistency?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chandu</title>
		<link>http://the-stickman.com/web-development/javascript/finding-selection-cursor-position-in-a-textarea-in-internet-explorer/comment-page-1/#comment-1412</link>
		<dc:creator>Chandu</dc:creator>
		<pubDate>Tue, 08 Jun 2010 12:54:14 +0000</pubDate>
		<guid isPermaLink="false">http://the-stickman.com/uncategorized/finding-selection-cursor-position-in-a-textarea-in-internet-explorer/#comment-1412</guid>
		<description>Hi,


document.selection.createRange(); not working for textbox. is there any alternative for it?




Regards,
Chandu</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>document.selection.createRange(); not working for textbox. is there any alternative for it?</p>
<p>Regards,<br />
Chandu</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stephen Andrew Carter</title>
		<link>http://the-stickman.com/web-development/javascript/finding-selection-cursor-position-in-a-textarea-in-internet-explorer/comment-page-1/#comment-1408</link>
		<dc:creator>Stephen Andrew Carter</dc:creator>
		<pubDate>Thu, 03 Jun 2010 13:58:36 +0000</pubDate>
		<guid isPermaLink="false">http://the-stickman.com/uncategorized/finding-selection-cursor-position-in-a-textarea-in-internet-explorer/#comment-1408</guid>
		<description>function get_selection(the_id)
{
    var e = document.getElementById(the_id);
    
    //Mozilla and DOM 3.0
    if(&#039;selectionStart&#039; in e)
    {
        var l = e.selectionEnd - e.selectionStart;
        return { start: e.selectionStart, end: e.selectionEnd, length: l, text: e.value.substr(e.selectionStart, l) };
    }
    //IE
    else if(document.selection)
    {
        e.focus();
        var r = document.selection.createRange();
        var tr = e.createTextRange();
        var tr2 = tr.duplicate();
        tr2.moveToBookmark(r.getBookmark());
        tr.setEndPoint(&#039;EndToStart&#039;,tr2);
        if (r == null &#124;&#124; tr == null) return { start: e.value.length, end: e.value.length, length: 0, text: &#039;&#039; };
        var text_part = r.text.replace(/[\r\n]/g,&#039;.&#039;); //for some reason IE doesn&#039;t always count the \n and \r in the length
        var text_whole = e.value.replace(/[\r\n]/g,&#039;.&#039;);
        var the_start = text_whole.indexOf(text_part,tr.text.length);
        return { start: the_start, end: the_start + text_part.length, length: text_part.length, text: r.text };
    }
    //Browser not supported
    else return { start: e.value.length, end: e.value.length, length: 0, text: &#039;&#039; };
}

function replace_selection(the_id,replace_str)
{
    var e = document.getElementById(the_id);
    selection = get_selection(the_id);
    var start_pos = selection.start;
    var end_pos = start_pos + replace_str.length;
    e.value = e.value.substr(0, start_pos) + replace_str + e.value.substr(selection.end, e.value.length);
    set_selection(the_id,start_pos,end_pos);
    return {start: start_pos, end: end_pos, length: replace_str.length, text: replace_str};
}

function set_selection(the_id,start_pos,end_pos)
{
    var e = document.getElementById(the_id);
    
    //Mozilla and DOM 3.0
    if(&#039;selectionStart&#039; in e)
    {
        e.focus();
        e.selectionStart = start_pos;
        e.selectionEnd = end_pos;
    }
    //IE
    else if(document.selection)
    {
        e.focus();
        var tr = e.createTextRange();
        
        //Fix IE from counting the newline characters as two seperate characters
        var stop_it = start_pos;
        for (i=0; i &lt; stop_it; i++) if( e.value[i].search(/[\r\n]/) != -1 ) start_pos = start_pos - .5;
        stop_it = end_pos;
        for (i=0; i &lt; stop_it; i++) if( e.value[i].search(/[\r\n]/) != -1 ) end_pos = end_pos - .5;
        
        tr.moveEnd(&#039;textedit&#039;,-1);
        tr.moveStart(&#039;character&#039;,start_pos);
        tr.moveEnd(&#039;character&#039;,end_pos - start_pos);
        tr.select();
    }
    return get_selection(the_id);
}

function wrap_selection(the_id, left_str, right_str, sel_offset, sel_length)
{
    var the_sel_text = get_selection(the_id).text;
    var selection =  replace_selection(the_id, left_str + the_sel_text + right_str );
    if(sel_offset !== undefined &amp;&amp; sel_length !== undefined) selection = set_selection(the_id, selection.start +  sel_offset, selection.start +  sel_offset + sel_length);
    else if(the_sel_text == &#039;&#039;) selection = set_selection(the_id, selection.start + left_str.length, selection.start + left_str.length);
    return selection;
}

//Your welcome :)</description>
		<content:encoded><![CDATA[<p>function get_selection(the_id)<br />
{<br />
    var e = document.getElementById(the_id);</p>
<p>    //Mozilla and DOM 3.0<br />
    if(&#8217;selectionStart&#8217; in e)<br />
    {<br />
        var l = e.selectionEnd &#8211; e.selectionStart;<br />
        return { start: e.selectionStart, end: e.selectionEnd, length: l, text: e.value.substr(e.selectionStart, l) };<br />
    }<br />
    //IE<br />
    else if(document.selection)<br />
    {<br />
        e.focus();<br />
        var r = document.selection.createRange();<br />
        var tr = e.createTextRange();<br />
        var tr2 = tr.duplicate();<br />
        tr2.moveToBookmark(r.getBookmark());<br />
        tr.setEndPoint(&#8216;EndToStart&#8217;,tr2);<br />
        if (r == null || tr == null) return { start: e.value.length, end: e.value.length, length: 0, text: &#8221; };<br />
        var text_part = r.text.replace(/[\r\n]/g,&#8217;.'); //for some reason IE doesn&#8217;t always count the \n and \r in the length<br />
        var text_whole = e.value.replace(/[\r\n]/g,&#8217;.');<br />
        var the_start = text_whole.indexOf(text_part,tr.text.length);<br />
        return { start: the_start, end: the_start + text_part.length, length: text_part.length, text: r.text };<br />
    }<br />
    //Browser not supported<br />
    else return { start: e.value.length, end: e.value.length, length: 0, text: &#8221; };<br />
}</p>
<p>function replace_selection(the_id,replace_str)<br />
{<br />
    var e = document.getElementById(the_id);<br />
    selection = get_selection(the_id);<br />
    var start_pos = selection.start;<br />
    var end_pos = start_pos + replace_str.length;<br />
    e.value = e.value.substr(0, start_pos) + replace_str + e.value.substr(selection.end, e.value.length);<br />
    set_selection(the_id,start_pos,end_pos);<br />
    return {start: start_pos, end: end_pos, length: replace_str.length, text: replace_str};<br />
}</p>
<p>function set_selection(the_id,start_pos,end_pos)<br />
{<br />
    var e = document.getElementById(the_id);</p>
<p>    //Mozilla and DOM 3.0<br />
    if(&#8217;selectionStart&#8217; in e)<br />
    {<br />
        e.focus();<br />
        e.selectionStart = start_pos;<br />
        e.selectionEnd = end_pos;<br />
    }<br />
    //IE<br />
    else if(document.selection)<br />
    {<br />
        e.focus();<br />
        var tr = e.createTextRange();</p>
<p>        //Fix IE from counting the newline characters as two seperate characters<br />
        var stop_it = start_pos;<br />
        for (i=0; i &lt; stop_it; i++) if( e.value[i].search(/[\r\n]/) != -1 ) start_pos = start_pos &#8211; .5;<br />
        stop_it = end_pos;<br />
        for (i=0; i &lt; stop_it; i++) if( e.value[i].search(/[\r\n]/) != -1 ) end_pos = end_pos &#8211; .5;</p>
<p>        tr.moveEnd(&#039;textedit&#039;,-1);<br />
        tr.moveStart(&#039;character&#039;,start_pos);<br />
        tr.moveEnd(&#039;character&#039;,end_pos &#8211; start_pos);<br />
        tr.select();<br />
    }<br />
    return get_selection(the_id);<br />
}</p>
<p>function wrap_selection(the_id, left_str, right_str, sel_offset, sel_length)<br />
{<br />
    var the_sel_text = get_selection(the_id).text;<br />
    var selection =  replace_selection(the_id, left_str + the_sel_text + right_str );<br />
    if(sel_offset !== undefined &amp;&amp; sel_length !== undefined) selection = set_selection(the_id, selection.start +  sel_offset, selection.start +  sel_offset + sel_length);<br />
    else if(the_sel_text == &#039;&#039;) selection = set_selection(the_id, selection.start + left_str.length, selection.start + left_str.length);<br />
    return selection;<br />
}</p>
<p>//Your welcome <img src='http://the-stickman.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: anonymous</title>
		<link>http://the-stickman.com/web-development/javascript/finding-selection-cursor-position-in-a-textarea-in-internet-explorer/comment-page-1/#comment-1398</link>
		<dc:creator>anonymous</dc:creator>
		<pubDate>Tue, 25 May 2010 13:09:05 +0000</pubDate>
		<guid isPermaLink="false">http://the-stickman.com/uncategorized/finding-selection-cursor-position-in-a-textarea-in-internet-explorer/#comment-1398</guid>
		<description>pretty helpfull...thanks a ton!!</description>
		<content:encoded><![CDATA[<p>pretty helpfull&#8230;thanks a ton!!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jerry B.</title>
		<link>http://the-stickman.com/web-development/javascript/finding-selection-cursor-position-in-a-textarea-in-internet-explorer/comment-page-1/#comment-1392</link>
		<dc:creator>Jerry B.</dc:creator>
		<pubDate>Wed, 12 May 2010 16:37:52 +0000</pubDate>
		<guid isPermaLink="false">http://the-stickman.com/uncategorized/finding-selection-cursor-position-in-a-textarea-in-internet-explorer/#comment-1392</guid>
		<description>Here is my solution, it moves a bookmark from the document.selection textrange to an element textrange. It correctly calculates the start and end positions regardless of where the element is in the page. 

function getSelectionRange(oElm)
{
	var $r	= { text: &quot;&quot;, start: 0, end: 0, length: 0 };
	if (oElm.setSelectionRange)
	{	// W3C/Gecko
		$r.start= oElm.selectionStart;
		$r.end	= oElm.selectionEnd;
		$r.text	= ($r.start != $r.end) ? oElm.value.substring($r.start, $r.end): &quot;&quot;;
	}
	else if (document.selection)
	{	// IE
		if (oElm.tagName &amp;&amp; oElm.tagName === &quot;TEXTAREA&quot;)
		{
			var $oS	= document.selection.createRange().duplicate();
			var $oR	= oElm.createTextRange();
			var $sB	= $oS.getBookmark();
			$oR.moveToBookmark($sB);
		}
		else
			var $oR	= document.selection.createRange().duplicate();
		$r.text	= $oR.text;
		for (; $oR.moveStart(&quot;character&quot;, -1) !== 0; $r.start++);
		$r.end	= $r.text.length + $r.start;
	}
	$r.length	= $r.text.length;
	return $r;
}</description>
		<content:encoded><![CDATA[<p>Here is my solution, it moves a bookmark from the document.selection textrange to an element textrange. It correctly calculates the start and end positions regardless of where the element is in the page. </p>
<p>function getSelectionRange(oElm)<br />
{<br />
	var $r	= { text: &#8220;&#8221;, start: 0, end: 0, length: 0 };<br />
	if (oElm.setSelectionRange)<br />
	{	// W3C/Gecko<br />
		$r.start= oElm.selectionStart;<br />
		$r.end	= oElm.selectionEnd;<br />
		$r.text	= ($r.start != $r.end) ? oElm.value.substring($r.start, $r.end): &#8220;&#8221;;<br />
	}<br />
	else if (document.selection)<br />
	{	// IE<br />
		if (oElm.tagName &amp;&amp; oElm.tagName === &#8220;TEXTAREA&#8221;)<br />
		{<br />
			var $oS	= document.selection.createRange().duplicate();<br />
			var $oR	= oElm.createTextRange();<br />
			var $sB	= $oS.getBookmark();<br />
			$oR.moveToBookmark($sB);<br />
		}<br />
		else<br />
			var $oR	= document.selection.createRange().duplicate();<br />
		$r.text	= $oR.text;<br />
		for (; $oR.moveStart(&#8220;character&#8221;, -1) !== 0; $r.start++);<br />
		$r.end	= $r.text.length + $r.start;<br />
	}<br />
	$r.length	= $r.text.length;<br />
	return $r;<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nux</title>
		<link>http://the-stickman.com/web-development/javascript/finding-selection-cursor-position-in-a-textarea-in-internet-explorer/comment-page-1/#comment-1385</link>
		<dc:creator>Nux</dc:creator>
		<pubDate>Sat, 10 Apr 2010 17:00:38 +0000</pubDate>
		<guid isPermaLink="false">http://the-stickman.com/uncategorized/finding-selection-cursor-position-in-a-textarea-in-internet-explorer/#comment-1385</guid>
		<description>Just a note as this is high in Google...

boundingLeft will NOT work at all in this situation - it gives a value in pixels not characters.

Stine version is also wrong as the text it self might contain &quot;01&quot;.

The original version seems to work almost perfectly, but I think setting the attributes is not a good idea as they won&#039;t be updated and some script might use them to detect compatibility.

I said &quot;almost perfectly&quot;, because to get a caret position you should run:
element.focus()</description>
		<content:encoded><![CDATA[<p>Just a note as this is high in Google&#8230;</p>
<p>boundingLeft will NOT work at all in this situation &#8211; it gives a value in pixels not characters.</p>
<p>Stine version is also wrong as the text it self might contain &#8220;01&#8243;.</p>
<p>The original version seems to work almost perfectly, but I think setting the attributes is not a good idea as they won&#8217;t be updated and some script might use them to detect compatibility.</p>
<p>I said &#8220;almost perfectly&#8221;, because to get a caret position you should run:<br />
element.focus()</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: surendra</title>
		<link>http://the-stickman.com/web-development/javascript/finding-selection-cursor-position-in-a-textarea-in-internet-explorer/comment-page-1/#comment-1377</link>
		<dc:creator>surendra</dc:creator>
		<pubDate>Thu, 18 Mar 2010 07:17:41 +0000</pubDate>
		<guid isPermaLink="false">http://the-stickman.com/uncategorized/finding-selection-cursor-position-in-a-textarea-in-internet-explorer/#comment-1377</guid>
		<description>But Those are not working in all browsers except IE</description>
		<content:encoded><![CDATA[<p>But Those are not working in all browsers except IE</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scott Alex</title>
		<link>http://the-stickman.com/web-development/javascript/finding-selection-cursor-position-in-a-textarea-in-internet-explorer/comment-page-1/#comment-1292</link>
		<dc:creator>Scott Alex</dc:creator>
		<pubDate>Sat, 29 Aug 2009 19:48:35 +0000</pubDate>
		<guid isPermaLink="false">http://the-stickman.com/uncategorized/finding-selection-cursor-position-in-a-textarea-in-internet-explorer/#comment-1292</guid>
		<description>Here is a better solution, supported by IE 4.0+

// Current selection
var range = document.selection.createRange();

if(range.text != &quot;&quot;) {

   // Calculate start and end points
   sourceSelEnd = range.boundingLeft;
   sourceSelStart = sourceSelEnd - range.text.length;
   sourceSelected = range.text;

}

Documentation:
http://msdn.microsoft.com/en-us/library/ms533539(VS.85).aspx</description>
		<content:encoded><![CDATA[<p>Here is a better solution, supported by IE 4.0+</p>
<p>// Current selection<br />
var range = document.selection.createRange();</p>
<p>if(range.text != &#8220;&#8221;) {</p>
<p>   // Calculate start and end points<br />
   sourceSelEnd = range.boundingLeft;<br />
   sourceSelStart = sourceSelEnd &#8211; range.text.length;<br />
   sourceSelected = range.text;</p>
<p>}</p>
<p>Documentation:<br />
<a href="http://msdn.microsoft.com/en-us/library/ms533539(VS.85).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/ms533539(VS.85).aspx</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Louis Hoefler</title>
		<link>http://the-stickman.com/web-development/javascript/finding-selection-cursor-position-in-a-textarea-in-internet-explorer/comment-page-1/#comment-1274</link>
		<dc:creator>Louis Hoefler</dc:creator>
		<pubDate>Wed, 22 Jul 2009 20:10:30 +0000</pubDate>
		<guid isPermaLink="false">http://the-stickman.com/uncategorized/finding-selection-cursor-position-in-a-textarea-in-internet-explorer/#comment-1274</guid>
		<description>getSelectionOffset : function(argObject) {
   if (typeof(argObject.contentWindow.getSelection) != &#039;undefined&#039;) { //Moz
    return {
     start: argObject.contentWindow.getSelection().getRangeAt(0).selectionStart,
     end: argObject.contentWindow.getSelection().getRangeAt(0).selectionEnd
    }
   }
   if (document.selection &amp;&amp; document.selection.createRange) { //IE
    var allText = argObject.contentWindow.document.selection.createRange().parentElement().innerText;
    var selText = argObject.contentWindow.document.selection.createRange().text;
    return {
     start: allText.indexOf(selText),
     end: allText.indexOf(selText) + selText.length 
    }
   }
  }</description>
		<content:encoded><![CDATA[<p>getSelectionOffset : function(argObject) {<br />
   if (typeof(argObject.contentWindow.getSelection) != &#8216;undefined&#8217;) { //Moz<br />
    return {<br />
     start: argObject.contentWindow.getSelection().getRangeAt(0).selectionStart,<br />
     end: argObject.contentWindow.getSelection().getRangeAt(0).selectionEnd<br />
    }<br />
   }<br />
   if (document.selection &amp;&amp; document.selection.createRange) { //IE<br />
    var allText = argObject.contentWindow.document.selection.createRange().parentElement().innerText;<br />
    var selText = argObject.contentWindow.document.selection.createRange().text;<br />
    return {<br />
     start: allText.indexOf(selText),<br />
     end: allText.indexOf(selText) + selText.length<br />
    }<br />
   }<br />
  }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: MeltingIce</title>
		<link>http://the-stickman.com/web-development/javascript/finding-selection-cursor-position-in-a-textarea-in-internet-explorer/comment-page-1/#comment-1219</link>
		<dc:creator>MeltingIce</dc:creator>
		<pubDate>Tue, 26 May 2009 08:55:32 +0000</pubDate>
		<guid isPermaLink="false">http://the-stickman.com/uncategorized/finding-selection-cursor-position-in-a-textarea-in-internet-explorer/#comment-1219</guid>
		<description>Just wanted to say THANK YOU SO MUCH.  It&#039;s almost 5am and I was about to go officially insane when I finally found this post and it saved me from breaking the closest thing within my reach XD

Works beautifully for me!</description>
		<content:encoded><![CDATA[<p>Just wanted to say THANK YOU SO MUCH.  It&#8217;s almost 5am and I was about to go officially insane when I finally found this post and it saved me from breaking the closest thing within my reach XD</p>
<p>Works beautifully for me!</p>
]]></content:encoded>
	</item>
</channel>
</rss>
