Here is the kickstart
var range = window.getSelection().getRangeAt(0);
var newElement = document.createElement('span');
newElement.id = 'myId';
newElement.innerHTML = 'Hello World!';
if(range.startContainer.parentNode.id==='myDiv') {
range.deleteContents();
range.insertNode(newElement);
}
I don't have IE, but it works fine on firefox, chrome and safari. You might want to play with range.startContainer to continue only if the selection is made on the contentEditable div.
: quirksmode window.getSelection(), IE.
var userSelection;
if (window.getSelection) {
userSelection = window.getSelection();
}
else if (document.selection) {
userSelection = document.selection.createRange();
}