| 1 |
/* |
| 2 |
insert text at the position of the cursor in a HTML textarea |
| 3 |
Version: 1.0 |
| 4 |
Original code: Arnan de Gans |
| 5 |
Copyright: (c) 2020 Arnan de Gans |
| 6 |
*/ |
| 7 |
function textatcursor(areaId,text) { |
| 8 |
var txtarea = document.getElementById(areaId); |
| 9 |
var scrollPos = txtarea.scrollTop; |
| 10 |
var strPos = 0; |
| 11 |
var br = ((txtarea.selectionStart || txtarea.selectionStart == '0') ? "ff" : (document.selection ? "ie" : false ) ); |
| 12 |
|
| 13 |
if(br == "ie") { |
| 14 |
txtarea.focus(); |
| 15 |
var range = document.selection.createRange(); |
| 16 |
range.moveStart ('character', -txtarea.value.length); |
| 17 |
strPos = range.text.length; |
| 18 |
} else if(br == "ff") { |
| 19 |
strPos = txtarea.selectionStart; |
| 20 |
} |
| 21 |
|
| 22 |
var front = (txtarea.value).substring(0,strPos); |
| 23 |
var back = (txtarea.value).substring(strPos,txtarea.value.length); |
| 24 |
txtarea.value=front+text+back; |
| 25 |
strPos = strPos + text.length; |
| 26 |
if(br == "ie") { |
| 27 |
txtarea.focus(); |
| 28 |
var range = document.selection.createRange(); |
| 29 |
range.moveStart ('character', -txtarea.value.length); |
| 30 |
range.moveStart ('character', strPos); |
| 31 |
range.moveEnd ('character', 0); |
| 32 |
range.select(); |
| 33 |
} else if(br == "ff") { |
| 34 |
txtarea.selectionStart = strPos; |
| 35 |
txtarea.selectionEnd = strPos; |
| 36 |
txtarea.focus(); |
| 37 |
} |
| 38 |
txtarea.scrollTop = scrollPos; |
| 39 |
} |