Created
June 10, 2012 20:16
-
-
Save maccman/2907189 to your computer and use it in GitHub Desktop.
This file contains 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
(function($){ | |
var insertAtCaret = function(value) { | |
if (document.selection) { // IE | |
this.focus(); | |
sel = document.selection.createRange(); | |
sel.text = value; | |
this.focus(); | |
} | |
else if (this.selectionStart || this.selectionStart == '0') { | |
var startPos = this.selectionStart; | |
var endPos = this.selectionEnd; | |
var scrollTop = this.scrollTop; | |
this.value = [ | |
this.value.substring(0, startPos), | |
value, | |
this.value.substring(endPos, this.value.length) | |
].join(''); | |
this.focus(); | |
this.selectionStart = startPos + value.length; | |
this.selectionEnd = startPos + value.length; | |
this.scrollTop = scrollTop; | |
} else { | |
throw new Error('insertAtCaret not supported'); | |
} | |
}; | |
$.fn.insertAtCaret = function(value){ | |
$(this).each(function(){ | |
insertAtCaret.call(this, value); | |
}) | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment