function setCaretPosition(oField, iCaretStart, emailText) { // IE Support if (document.selection) { // Focus on the text box oField.focus(); // This returns us an object containing // information about the currently selected text var oSel = document.selection.createRange(); // Move selection start and end to 0 position oSel.moveStart ('character', -oField.value.length); // Move selection start and end to desired position oSel.moveStart ('character', iCaretStart); //oSel.moveEnd ('character', 0); oSel.pasteHTML(emailText); oField.value = oSel.value ; } // Firefox support else if(oField.selectionStart || oField.selectionStart == '0') { // the text that is in the email box var text = oField.value; // the text before the caret var prevText = oField.value.substring(0, iCaretStart); // the text after the caret var nextText = oField.value.substring(iCaretStart); // the new email box text oField.value = prevText + '\n' + emailText + '\n' + nextText ; oField.focus(); } }