Fmdpa Posted May 25, 2011 Share Posted May 25, 2011 How do I insert text at the insertion point in a textarea or input? Link to comment Share on other sites More sharing options...
MrFish Posted May 25, 2011 Share Posted May 25, 2011 Use textarea.selectionStart to get the caret location. Then you can get the value of the textarea, split it into 2 strings, and insert the text in-between. textarea = document.getElementById("textarea");var caretPos = textarea.selectionStart;var text = textarea.value;var first = text.substring(0, caretPos);var second = text.substring(caretPos, text.length);var insertionString = "text to add";var newString = first + insertionString + last;textarea.value = newString;var newPos = first.length + insertionString.length;textarea.setSelectionRange(newPos, newPos); That should work. Link to comment Share on other sites More sharing options...
jeffman Posted May 25, 2011 Share Posted May 25, 2011 Cross-browser compatibility? Link to comment Share on other sites More sharing options...
Fmdpa Posted May 26, 2011 Author Share Posted May 26, 2011 I don't need cross-browser compatibility for this application, but the knowledge may be useful for the future. Thanks for the reply MrFish! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.