Possible Duplicate:
Insert text at cursor in a content editable div
I have this code / jQuery Plugin.
jQuery.fn.extend({
insertAtCaret: function(myValue) {
return this.each(function(i) {
if (document.selection) {
//For browsers like Internet Explorer
this.focus();
sel = document.selection.createRange();
sel.text = myValue;
this.focus();
}
else if (this.selectionStart || this.selectionStart == '0') {
//For browsers like Firefox and Webkit based
var startPos = this.selectionStart;
var endPos = this.selectionEnd;
var scrollTop = this.scrollTop;
this.value = this.value.substring(0, startPos) + myValue + this.value.substring(endPos, this.value.length);
this.focus();
this.selectionStart = startPos + myValue.length;
this.selectionEnd = startPos + myValue.length;
this.scrollTop = scrollTop;
} else {
this.value += myValue;
this.focus();
}
})
}
});
This code functions as to insert a non-default character at the caret. Eg, somebody presses the tab key, it inserts \t or they press the E button and it inserts a P instead. The issue I am having is that this code works beautifully in a textarea, but when I try to use it on a contentEditable div, nothing happens. So could I modify this code to work with a contentEditable div instead? Thanks!
EDIT
Demo here: http://jsbin.com/esiced/9/edit