Click here to Skip to main content
15,886,049 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
XML
<HTML>
<HEAD><TITLE>Selection</TITLE>
<SCRIPT type="text/javascript">
function disp()
{
  var text = document.getElementById("text");
  //var t = text.value.substr(text.selectionStart,text.selectionEnd-text.selectionStart);
  //alert(t+"\nSelectionStart:"+text.selectionStart);
  

}
</SCRIPT>
</HEAD>
<BODY>

<TEXTAREA id="text">Hello, How are You?</TEXTAREA><BR>
<INPUT type="button"  value="Select text and click here"   önclick="disp();"/>
</BODY>
</HTML>



SelectionStart is not Work In IE but In FireFox Work.
Please Help me
Posted
Updated 2-Aug-18 19:28pm
Comments
Sandeep Mewara 4-Aug-12 16:24pm    
Can you elaborate a little more?

Source: http://stackoverflow.com/questions/1891444/how-can-i-get-cursor-position-in-a-textarea[^]

for cross-browser compatibility you will need to test for a few capabilities:

1. selectionStart (which you have already found out)
2. selection (which is shown in the link above)

essential code:
C#
var pos = 0;
if("selectionStart" in el) {
   pos = el.selectionStart;
} else if("selection" in document) {
   el.focus();
   var Sel = document.selection.createRange();
   var SelLength = document.selection.createRange().text.length;
   Sel.moveStart("character", -el.value.length);
   pos = Sel.text.length - SelLength;
}
return pos;
 
Share this answer
 
set autofocus="autofocus" in the textbox. This will work in HTML 5.
 
Share this answer
 
JavaScript
let cursorIndex = $('#textAreaId')[0].selectionStart;
 
Share this answer
 
Comments
Richard Deeming 3-Aug-18 13:29pm    
SIX YEARS too late.

The "selectionStart" property wasn't supported in all browsers when this question was posted. But it was included in Solution 1, which is far more robust than your solution.

And using jQuery to get a single element by ID, rather than using document.getElementById, is pointless.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900