0
function insertText(elemID, text) {
    var elem = document.getElementById(elemID);
    elem.innerHTML += text;
}

This function works well to use buttons to add strings to a textarea in order to create a complex paragraph.

However, if the user adds/deletes/modifies that text with the keyboard, the function will no longer work until the form is refreshed, which loses the text changes.

Is there a way to modify the function to allow resuming use of the function after any keyboard edits without losing the text that has been added and modified?

tiredeyes
  • 13
  • 3

1 Answers1

1

Basically a non-jQuery duplicate of Why is my text not being added to the textarea after the second change event? .

innerHTML sets the default value of the textarea. Once the textarea was edited by the user, the current value is stored in the value property.

Use

elem.value += text;

instead.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143