jquery-en  

[jQuery] Re: Inserting text into TextArea at location

variaas
Sat, 23 Jun 2007 15:21:46 -0700

to all...I'm using a function written by Alex King, and adapted it to
a function in jQuery:

/**
 * Insert content at caret position (converted to jquery function)
 * @link 
http://alexking.org/blog/2003/06/02/inserting-at-the-cursor-using-javascript
 */
$.fn.insertAtCaret = function (myValue) {
        return this.each(function(){
                //IE support
                if (document.selection) {
                        this.focus();
                        sel = document.selection.createRange();
                        sel.text = myValue;
                        this.focus();
                }
                //MOZILLA/NETSCAPE support
                else if (this.selectionStart || this.selectionStart == '0') {
                        var startPos = this.selectionStart;
                        var endPos = this.selectionEnd;
                        var scrollTop = this.scrollTop;
                        this.value = this.value.substring(0, startPos)
                                      + myValue
                              + this.value.substring(endPos,
this.value.length);
                        this.focus();
                        this.selectionStart = startPos + myValue.length;
                        this.selectionEnd = startPos + myValue.length;
                        this.scrollTop = scrollTop;
                } else {
                        this.value += myValue;
                        this.focus();
                }
        });

};

On Jun 22, 10:46 am, variaas <[EMAIL PROTECTED]> wrote:
> How do I insert a string at a specific place in a textarea? I want to
> insert the string where ever the keyboard cursor is.