Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I insert some text where the cursor is? [duplicate]

I'm upgrading some Javascript which works on IE. However, I'm having some problems.

Heres the IE code:

var range = document.getElementById('text').contentWindow.window
                 .document.getElementById('Content').createTextRange();
var textObj = document.getElementById('text').contentWindow.window
                 .document.getElementById('Content');

var textFieldValue = theSmile;
if (range && textObj.CursorPos) {
    var CursorPos = textObj.CursorPos;
    CursorPos.text = CursorPos.text.charAt(CursorPos.text.length - 1) 
                              == ' ' ?' ' + textFieldValue : textFieldValue;
} else {
  textObj.value  = textFieldValue;
}

I've tried replacing CreateTextRange with CreateRange for non-IE browsers, but this doesn't help. With code like this:

var range;
var textObj;
var iframeEl = document.getElementById('text');
if (iframeEl.contentDocument) { // DOM
    range = iframeEl.contentDocument.getElementById('Content').createRange;
    textObj= iframeEl.contentDocument.getElementById('Content');
} else if (iframeEl.contentWindow) { // IE win
    range = iframeEl.contentWindow.document.getElementById('Content')
                                                            .createTextRange;
    textObj= iframeEl.contentWindow.document.getElementById('Content');
}
like image 432
Jules Avatar asked Sep 13 '11 15:09

Jules


People also ask

Is the position of cursor where we insert text?

First, get the current position of cursor with the help of property named as selectionStart on textarea/inputbox. To insert the text at the given position we will use slice function to break the string into two parts and then we will append both parts to the text(text_to_insert) in front and end of the text.

How do you insert text into the textarea at the current cursor position react JS?

Use the setRangeText Method document. activeElement is the current element that's focused on. In the function, we get the selection start and selection end with selectionStart and selectionEnd . Then we call setRangeText with the newText , start , end and 'selection' to add the newText at the current cursor position.


1 Answers

Here's a function to insert text at the cursor in a textarea or text input, which is what it seems you have. It works in all major browsers:

function insertTextAtCursor(el, text) {
    var val = el.value, endIndex, range, doc = el.ownerDocument;
    if (typeof el.selectionStart == "number"
            && typeof el.selectionEnd == "number") {
        endIndex = el.selectionEnd;
        el.value = val.slice(0, endIndex) + text + val.slice(endIndex);
        el.selectionStart = el.selectionEnd = endIndex + text.length;
    } else if (doc.selection != "undefined" && doc.selection.createRange) {
        el.focus();
        range = doc.selection.createRange();
        range.collapse(false);
        range.text = text;
        range.select();
    }
}

You can use it as follows:

var iframeWin = document.getElementById('text').contentWindow;
var textObj = iframeWin.document.getElementById('Content');

insertTextAtCursor(textObj, "foo");
like image 81
Tim Down Avatar answered Oct 12 '22 01:10

Tim Down