function setCaretPosition(elemId, caretPos) { var elem = document.getElementById(elemId); if(elem != null) { if(elem.createTextRange) { var range = elem.createTextRange(); range.move('character', caretPos); range.select(); } else { if(elem.selectionStart) { elem.focus(); elem.setSelectionRange(caretPos, caretPos); } else elem.focus(); } } }The first expected parameter is the ID of the element you wish to insert the cursor on. If the element is unable to be found, nothing will happen (obviously). The second parameter is the caret positon index. Zero will put the cursor at the beginning. If you pass a number larger than the number of characters in the elements value, it will put the cursor at the end.
Setting Cursor Position in TextBox with Javascript
Javascript Benedict Alphonse Thursday, May 28, 2009 0 comments
Subscribe to:
Post Comments (RSS)
0 Responses to "Setting Cursor Position in TextBox with Javascript"
Post a Comment