Jump to content

Insert Text at the Insertion Point


Fmdpa

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...