Jump to content

Insert Text at the Insertion Point


Fmdpa
 Share

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...