Get cursor postion in characters in textarea - Javascript

How can I get the current cursor position in textbox / textarea by using Javascript function ?
0
give a positive ratinggive a negative rating
Hi,

To get the current cursor position in Javascript, you can use this solution:



<script>

function cursorPosition() {
var content = document.getElementById('text');
if((content.selectionStart!=null)&&(content.selectionStart!=undefined)){
var position = content.selectionStart;
document.getElementById('pos').innerHTML=position;
}
else {
return false;
}
}

</script>


<textarea id='text' style='width: 300px; height: 150px;' ></textarea>
<br />
<a href='javascript:void(0);' onClick='javascript: cursorPosition();' >Get cursor position</a>
<br />
<div id='pos'></div>



The function may not be supported in all browsers, but it works in the "relevant ones".

Share on FacebookShare on TwitterShare on LinkedInSend email
1 answer
x
x
2024 AnswerTabsTermsContact us