Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript insert text in textarea at cursor position

function insertAtCaret(areaId, text) {
  var txtarea = document.getElementById(areaId);
  var strPos = 0;
  var br = ((txtarea.selectionStart || txtarea.selectionStart == '0') ?
            "ff" : (document.selection ? "ie" : false));
  if (br == "ie") {
    txtarea.focus();
    var range = document.selection.createRange();
    range.moveStart('character', -txtarea.value.length);
    strPos = range.text.length;
  } else if (br == "ff") strPos = txtarea.selectionStart;

  var front = (txtarea.value).substring(0, strPos);
  var back = (txtarea.value).substring(strPos, txtarea.value.length);
  txtarea.value = front + text + back;
  strPos = strPos + text.length;
  if (br == "ie") {
    txtarea.focus();
    var range = document.selection.createRange();
    range.moveStart('character', -txtarea.value.length);
    range.moveStart('character', strPos);
    range.moveEnd('character', 0);
    range.select();
  } else if (br == "ff") {
    txtarea.selectionStart = strPos;
    txtarea.selectionEnd = strPos;
    txtarea.focus();
  }
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: jquery countdown timer 
Javascript :: o que é jsonm 
Javascript :: find my url in nodejs 
Javascript :: what is div in javascript 
Javascript :: append line break javascript 
Javascript :: hard refresh javascript 
Javascript :: find property in nested object 
Javascript :: javascript reverse 
Javascript :: dynamically change meta tags javascript 
Javascript :: how to use saved image on react 
Javascript :: npm fs 
Javascript :: convert a string to an array javascript 
Javascript :: js refresh 
Javascript :: jqery first img src 
Javascript :: js show element with focus 
Javascript :: what is node.js 
Javascript :: user icon discord js 
Javascript :: preventdefault not working form submit react 
Javascript :: nodemailer send email 
Javascript :: from string to number js 
Javascript :: js - change div height on scroll 
Javascript :: javascript combobox 
Javascript :: svg component react js 
Javascript :: removeeventlistener click 
Javascript :: jquery input hidden value 
Javascript :: jquery add to array with key 
Javascript :: flatten an array javascript 
Javascript :: untrack package-lock.json 
Javascript :: javascript unselect radio 
Javascript :: mdn clonenode 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =