/* add text to where the cursor is located in a textarea (and focus on it) this also replaces any selected text! this function taken from https://stackoverflow.com/a/11077016 with minor changes myField: which textarea to change (in this file, it's gonna be COMMENT) myValue: what text to add */ function insertAtCursor(myField, myValue) { //IE support if (document.selection) { myField.focus(); sel = document.selection.createRange(); sel.text = myValue; } //MOZILLA and others else if (myField.selectionStart || myField.selectionStart === 0) { var startPos = myField.selectionStart; var endPos = myField.selectionEnd; myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length); myField.selectionStart = startPos + myValue.length; myField.selectionEnd = startPos + myValue.length; myField.focus(); } else { myField.value += myValue; } } function insertThisInThere(thisChar, thereId) { /* function theCursorPosition(ofThisInput) { // set a fallback cursor location var theCursorLocation = 0; // find the cursor location via IE method... if (document.selection) { ofThisInput.focus(); var theSelectionRange = document.selection.createRange(); theSelectionRange.moveStart('character', -ofThisInput.value.length); theCursorLocation = theSelectionRange.text.length; } else if (ofThisInput.selectionStart || ofThisInput.selectionStart == '0') { // or the FF way theCursorLocation = ofThisInput.selectionStart; } return theCursorLocation; } // now get ready to place our new character(s)... var currentPos = theCursorPosition(theIdElement); var origValue = theIdElement.value; var newValue = origValue.substr(0, currentPos) + thisChar + origValue.substr(currentPos); */ //theIdElement.value = newValue; var theIdElement = document.getElementById(thereId); insertAtCursor(theIdElement, thisChar); }