javascript - Insert character at Cursor position in VUE JS - Stack Overflow

PHOTO EMBED

Mon Mar 22 2021 03:59:26 GMT+0000 (UTC)

Saved by @stewardjornsen #javascript

// methods:
insertSomething: function(insert) {
  const self = this;
  var tArea = this.$refs.yourTextarea;
  // filter:
  if (0 == insert) {
    return;
  }
  if (0 == cursorPos) {
    return;
  }

  // get cursor's position:
  var startPos = tArea.selectionStart,
    endPos = tArea.selectionEnd,
    cursorPos = startPos,
    tmpStr = tArea.value;

  // insert:
  self.txtContent = tmpStr.substring(0, startPos) + insert + tmpStr.substring(endPos, tmpStr.length);

  // move cursor:
  setTimeout(() => {
    cursorPos += insert.length;
    tArea.selectionStart = tArea.selectionEnd = cursorPos;
  }, 10);
}
content_copyCOPY

https://stackoverflow.com/questions/48145727/insert-character-at-cursor-position-in-vue-js