equal
deleted
inserted
replaced
|
1 |
|
2 function insertText(text, areaId) { |
|
3 var textarea = jQuery('#' + areaId); |
|
4 if (document.selection) { // IE |
|
5 var selLength; |
|
6 textarea.focus(); |
|
7 sel = document.selection.createRange(); |
|
8 selLength = sel.text.length; |
|
9 sel.text = text; |
|
10 sel.moveStart('character', selLength-text.length); |
|
11 sel.select(); |
|
12 } else if (textarea.selectionStart || textarea.selectionStart == '0') { // mozilla |
|
13 var startPos = textarea.selectionStart; |
|
14 var endPos = textarea.selectionEnd; |
|
15 // insert text so that it replaces the [startPos, endPos] part |
|
16 textarea.value = textarea.value.substring(0,startPos) + text + textarea.value.substring(endPos,textarea.value.length); |
|
17 // set cursor pos at the end of the inserted text |
|
18 textarea.selectionStart = textarea.selectionEnd = startPos+text.length; |
|
19 textarea.focus(); |
|
20 } else { // safety belt for other browsers |
|
21 textarea.value += text; |
|
22 } |
|
23 } |