Here is my solutions:
function getposition(txt1)
{
try //ie
{
var position=0;
var currentRange=document.selection.createRange();
var workRange=currentRange.duplicate();
txt1.select();
var allRange=document.selection.createRange();
while(workRange.compareEndPoints("StartToStart",allRange)>0)
{
workRange.moveStart("character",-1);
position++;
}
currentRange.select();
return position;
}
catch(Err) //firefox
{
return txt1.selectionStart;
}
}
Usage:
var cursorPosition = GetPosition(document.getElementById('<%= txtTextBoxNameInASPdotNET.ClientID %>'));
Note: To get an element's id in JavaScript from an ASP.net control use the following javascript and inline asp.net code:
var txt1=document.getElementById('<%= [asp.net control name].ClientID %>');
I found the following solution here:
No comments:
Post a Comment