Solution: See this JavaScript TextArea Auto Resize Program, Resize Text Area Automatically. Hey everyone. Alex you are awesome! I was having major issues with IE document selection. In this tutorial I will show you how to make HTML textarea element fixed size. The "selectionStart" property wasn't supported in all browsers when this question was posted. For example a comment on a review or feedback form. Extra special thanks to Saul on Stackoverflow for the “Keypress” idea!!! function getCursorPos(id) { It doesn’t have to measure or explicitly set height. } Obviously, this would generate some negative feedback from users where text is being appended and they are forced to manually scroll to the bottom each time. Here are some of the most important: Here’s an example of how some of these new attributes can be used to control the behavior of a textarea. } if (ctrl.createTextRange) { Perhaps something is wrong with my html code? } else { Here is slightly modified code compatible to IE 6.0 and Opera: function GetCaretPosition(control) { var CaretPos = 0; // IE Support if (document.selection) { control.focus(); var Sel = document.selection.createRange (); var Sel2 = Sel.duplicate(); Sel2.moveToElementText(control); var CaretPos = -1; while(Sel2.inRange(Sel)) { Sel2.moveStart(‘character’); CaretPos++; } }. The CSS position property defines, as the name says, how the element is positioned on the web page.. var keyCode = (window.event) ? pos = pos – sel.text.length; Then you will notice that the result being return is totally false. Great work…could you please tell me How to find the line no and column no of the cursor. How To Use The To Make Links & Open Them Where You Want! if (window.event.keycode57) but the AdSense on this page is annoying.. I wish I had thought of that. cursorPos++; Nice simple information I have been looking for. You saved my huge time, thanks a lot again and again. Html Attributes of Html.TextArea. Textarea Caret Position. If the start and end value are the same, it means that there's no selected text and the start value (or end value) is the position of the cursor. Previously I have shared some text related programs, but this is a textarea resize program. Note, the position … break; But it was included in Solution 1, which is far more robust than your solution. }, function $(el) { Explaining moveEnd and moveStart The above code could then be updated to use the equivalent getSelectionRange of the textarea when a user clicks a button, insert your code, then move the cursor to the end of the newly inserted code. (this.userAgent.indexOf(“Safari”) == -1); } Affiliate Links - Advertising Disclosure. The position is off by one for each Enter hit. } I see tinyMce uses this kind of function and I was looking to replicate the behaviour in other input fields. $(‘lblEnd’).innerHTML = sel.end; while(newRange.inRange(oldRange)) { el.value.substring(iCaretPos).split(‘\n’).length. I wasted about 3 days trying to understand othermechanisms to get around problems (some of) associated with IE V6. else if (control.selectionStart || control.selectionStart == ‘0’) CaretPos = control.selectionStart; Thanx a lot, that’s what I needed – it works nice with Opera 8.5 as well, ups. return true; window.event.cancelBubble = true It made IE freeze inside the “moving selection one step loop”. Permalink to comment # July 19, 2010. This property holds whether the TextArea has partial text input from an input method. ‘isIE7’: function() { return ! Only your code worked for me! Approach 1: Get the scrollHeight property of the textarea. (this.userAgent.indexOf(“Firefox”) == -1); }, What does HTML5 Textarea Attributes: Here's What You Should Know do? The original solution selects all text, all the time, which can be frustrating for users that only want to select some of the text. A text area can hold an unlimited number of characters, and the text renders in a fixed-width font (usually Courier). control = document.getElementById(id); Adds an event listener to a with any contents inserted between the opening and closing tags. return cursorPos; I posted few lines wrong, but now i checked it and it is working with IE. But if i call function doGetCaretPosition() after blur or keydown or keyup or click on textare it begins to behave “irrationally” showing wrong results (in text but if the cursor is on the end of a text it works correct). cursorPos++; This code is used by Twitter (as per their developer). I’m trying to implement an autofill script in the textarea and need to find the exact position. Get the top and left coordinates of the caret in a