Then it finally worked for IE, FF and Safari in both text areas and input texts! Thanks for any help. range.collapse(true); Your email address will not be published. var caretPos = -1 + pos; This code is used by Twitter (as per their developer). It’s does’t work in Chrome. Javascript - How to set and read the caret position (input cursor) in text elements. Great work…could you please tell me How to find the line no and column no of the cursor. The solution was in combining many parts of multiple answers found on the web (i.s no one solution seemed to work with Ajax. selection is collapsed), you will see only single value. I’m not sure exactly why but IE must remove them from the selection or something. I'm on a pathetic computer for which Mozilla does not exists, so I can't test ...But what about this?http://www.din.or.jp/~hagi3/JavaScript/JSTips/Mozilla/Samples/NSHTMLTextAreaElement.htmThe setSelectionRange would to me sound like something to try. var oldRange = document.selection.createRange (); When i apply the code above so as it is it work perfect. var sel = getCursorPosition(textArea, pos); I missed this:, I have this:function getCursorPos(textElement){ var cursorPos = -1; if (textElement && textElement.createTextRange) { var range = document.selection.createRange().duplicate(); range.setEndPoint(‘StartToEnd’,range); var start = document.body.createTextRange(); start.moveToElementText(textElement); cursorPos = calcBookmark(range.getBookmark())-calcBookmark(start.getBookmark()); var rLen = 0; do{ var BrLen = rLen; rLen = newLine(textElement.value.substring(0,cursorPos + rLen + 1)); }while(BrLen!=rLen); cursorPos += rLen; } return cursorPos;}function calcBookmark(bk){ return (bk.charCodeAt(0)-1)+(bk.charCodeAt(3)-1)*65536+(bk.charCodeAt(2)-1);}It work’s for textarea in IE. The code above didn’t work for me on IE. Sign in to view Worked for me except in Chrome.Saved my time. Ad Choices, http://www.din.or.jp/~hagi3/JavaScript/JSTips/Mozilla/Samples/NSHTMLTextAreaElement.htm. I wonder how to get/set the caret position of a textfield. e.stopPropagation : window.event.cancelBubble = true; Specifically, I have a an AJAX updatepanel with dropdown list and an “insert” button. Clif. cursorPos = control.selectionStart; }. I’ve verified that the ID and pos is passing correctly but doesn’t seem to be generating the right element id or something as the caret never appears. Thank u for your solution. what’s wrong? }, function registerTimeField(field) { so, when i select the row on the grid, the dropdown always gets the focus. if (document.selection) { // IE… The user selects a value from the dropdown list, clicks the Insert button, and the value is appended to the value in the textbox. So far, i can only get the Selected Item value to insert at the end of the text. how can I paste some text in the current position assigned by those script? Replied by SBajra. } ‘isSafari’: function() { return ! cursorPosSaved[control.id] = cursorPos; This interface doesn't inherit any properties. if (browser.isFF() == 1) { return(“FF”); } var r = (oTextbox.createTextRange()); An index greater than the length of the element's value is treated as pointing to the end of the value. It can save and restore the caret position. selectionEnd 1. it selects the data fine. }, kindly dont block the freakin content with the ads. I see tinyMce uses this kind of function and I was looking to replicate the behaviour in other input fields. else if (browser.isIE() == 1) { return (“IE”); } pos = data.indexOf(‘Hello World’); This is only for IE, I hope this helps… TextArea: This is a cat. } else { Thanks! Get the top and left coordinates of the caret in a