5

I have found JQuery InsertAtCaret Function Here But there is no detail given how to use it. I have tried a lot to understand that how it can be used, but could not find any way. Here is the function.

$.fn.insertAtCaret = function(myValue) {
    return this.each(function() {
        var me = this;
        if (document.selection) { // IE
            me.focus();
            sel = document.selection.createRange();
            sel.text = myValue;
            me.focus();
        } else if (me.selectionStart || me.selectionStart == '0') { // Real browsers
            var startPos = me.selectionStart, endPos = me.selectionEnd, scrollTop = me.scrollTop;
            me.value = me.value.substring(0, startPos) + myValue + me.value.substring(endPos, me.value.length);
            me.focus();
            me.selectionStart = startPos + myValue.length;
            me.selectionEnd = startPos + myValue.length;
            me.scrollTop = scrollTop;
        } else {
            me.value += myValue;
            me.focus();
        }
    });
};

I have a textbox input field and a textarea below it. Where Should I call this function and what value should I give it. And Where I have to give the reference of my textarea.

Rashid Farooq
  • 365
  • 5
  • 17
  • 1
    you might want to try leaving a comment on [the gist at github](https://gist.github.com/mathiasbynens/326491). Often, the author of a script will be more than happy to explain its use, and sometimes thrilled to know that their code is helping peiple. – Zach Lysobey Apr 12 '13 at 16:03
  • 2
    Looking at the code it just add the text contained in `myValue` at the current caret position. I guess something like `$('#myTextarea').insertAtCaret('my new text')` should work – ulentini Apr 12 '13 at 16:03

2 Answers2

6

Here is modified version of above plugin:

jQuery.fn.extend({
insertAtCaret: function(myValue){
  return this.each(function(i) {
    if (document.selection) {
      //For browsers like Internet Explorer
      this.focus();
      sel = document.selection.createRange();
      sel.text = myValue;
      this.focus();
    }
    else if (this.selectionStart || this.selectionStart == '0') {
      //For browsers like Firefox and Webkit based
      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();
    }
  })
}
});

Basically, this plugin allows you to insert a piece of text at caret of multiple textbox or textarea . You can use it like this:

 $('#element1, #element2, #element3, .class-of-elements').insertAtCaret('text');

Working Demo

Eli
  • 14,779
  • 5
  • 59
  • 77
0

Firefox recently broke the above code for me, I had to make changes.

jQuery.fn.extend({insertAtCaret: function(myValue){
    return this.each(function(i) {
        if (document.selection) {
            //For browsers like Internet Explorer
            //log("INSERT IE: " + myValue);
            this.focus();
            var sel = document.selection.createRange();
            sel.text = myValue;
            this.focus();
        } else if (this.selectionStart || this.selectionStart == '0') {
            //For browsers like Firefox and Webkit based
            //log("INSERT Firefox: " + myValue);
            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 if($(this).hasClass("ql-editor")){
            //log("INSERT QL-EDITOR: " + myValue);
            var range = window.getSelection().getRangeAt(0);
            var startPos = range.startOffset;
            var endPos = range.endOffset;
            var value = $(this).html();
            $(this).html(value.substring(0, startPos) + myValue + value.substring(endPos, value.length));
            this.focus();
        } else {
            //log("INSERT Unknown: " + myValue);
            this.value += myValue;
            this.focus();
        }
        //log(this);
    });
}});
Dharman
  • 30,962
  • 25
  • 85
  • 135
NeoTechni
  • 158
  • 2
  • 10