3 * Allows inserting text where the caret is in a textarea
4 * Copyright (c) 2003-2010 phpMyAdmin devel team
6 * Developed by the phpMyAdmin devel team. Modified by Alex King and variaas
7 * http://alexking.org/blog/2003/06/02/inserting-at-the-cursor-using-javascript
8 * http://www.mail-archive.com/jquery-en@googlegroups.com/msg08708.html
9 * Licensed under the GPL license:
10 * http://www.gnu.org/licenses/gpl.html
14 $.fn.insertAtCaret = function (myValue) {
16 return this.each(function() {
19 if (document.selection) {
22 sel = document.selection.createRange();
26 } else if (this.selectionStart || this.selectionStart == '0') {
28 //MOZILLA / NETSCAPE support
29 var startPos = this.selectionStart;
30 var endPos = this.selectionEnd;
31 var scrollTop = this.scrollTop;
32 this.value = this.value.substring(0, startPos)+ myValue+ this.value.substring(endPos,this.value.length);
34 this.selectionStart = startPos + myValue.length;
35 this.selectionEnd = startPos + myValue.length;
36 this.scrollTop = scrollTop;
40 this.value += myValue;