Get the offset position of the caret in a textarea in pixels [duplicate]

You can create a separate (invisible) element and fill it with textarea content from start to the cursor position. Textarea and the “clone” should have matching CSS (font properties, padding/margin/border and width). Then stack these elements on top of each other. Let me start with a working example, then walk through the code: http://jsfiddle.net/g7rBk/ Updated … Read more

Is there anyway to have a textarea “autofit” height based on the content at page load?

How about http://www.jacklmoore.com/autosize/ Drop Autosize into any web page and it should Just Work. The source is short and well commented if you are curious to how it works. // Example: $(document).ready(function(){ $(‘textarea’).autosize(); }); Source: https://github.com/jackmoore/autosize Demo: http://www.jacklmoore.com/autosize/

How can you move the cursor to the last position of a textarea in Javascript?

xgMz’s answer was best for me. You don’t need to worry about the browser: var html = $(“#MyTextArea”).val(); $(“#MyTextArea”).focus().val(“”).val(html); And here’s a quick jQuery extension I wrote to do this for me next time: ; (function($) { $.fn.focusToEnd = function() { return this.each(function() { var v = $(this).val(); $(this).focus().val(“”).val(v); }); }; })(jQuery); Use like this: … Read more

Enter key in textarea

You could do something like this: <body> <textarea id=”txtArea” onkeypress=”onTestChange();”></textarea> <script> function onTestChange() { var key = window.event.keyCode; // If the user has pressed enter if (key === 13) { document.getElementById(“txtArea”).value = document.getElementById(“txtArea”).value + “\n*”; return false; } else { return true; } } </script> </body> Although the new line character feed from pressing enter … Read more