Here is a function to insert text into the cursor in a text box or text input, which, as you think, 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");
Tim Down Sep 13 '11 at 15:48 2011-09-13 15:48
source share