Created
March 24, 2011 19:29
-
-
Save Phrogz/885683 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// From SMF | |
// Replaces the currently selected text with the passed text. | |
function replaceText(text, textarea) | |
{ | |
// Attempt to create a text range (IE). | |
if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange) | |
{ | |
var caretPos = textarea.caretPos; | |
caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text; | |
caretPos.select(); | |
} | |
// Mozilla text range replace. | |
else if (typeof(textarea.selectionStart) != "undefined") | |
{ | |
var begin = textarea.value.substr(0, textarea.selectionStart); | |
var end = textarea.value.substr(textarea.selectionEnd); | |
var scrollPos = textarea.scrollTop; | |
textarea.value = begin + text + end; | |
if (textarea.setSelectionRange) | |
{ | |
textarea.focus(); | |
textarea.setSelectionRange(begin.length + text.length, begin.length + text.length); | |
} | |
textarea.scrollTop = scrollPos; | |
} | |
// Just put it on the end. | |
else | |
{ | |
textarea.value += text; | |
textarea.focus(textarea.value.length - 1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment