Friday, 5 July 2013

How to Find Position Of Cursor In Text Box in javascript


Write Following Codes to get the position of cursor in text box

<!doctype>
<html>
<head>
<script type="text/javascript">
function getPos()
{
var input = document.getElementById("txt");
       if (!input) return; // No (input) element found
       if (document.selection) {
           // IE
          input.focus();
       }
      alert(('selectionStart' in input ? input.selectionStart:'' || Math.abs(document.selection.createRange().moveStart('character', -input.value.length))));

}
</script>
</head>
<input type="text" id="txt"/>
<button onclick=" getPos();">Find</button>
</html>

Demo:-

 Find Position

No comments:

Post a Comment