I'm using window.getSelection().anchorOffset and focusOffset to turn selected area into an element. It works fine the first time. But then the parent element divided by new element and getSelection() start count from new element. I don't know how to get current element of getSelection(), this is the only way I can think of to fix this.
var temp = window.getSelection();
if (temp.anchorOffset < temp.focusOffset) {
var start = temp.anchorOffset;
var end = temp.focusOffset;
}else{
var end = temp.anchorOffset;
var start = temp.focusOffset;
}
var mid = text.substring(start,end);
$(".main-element").html(text.substring(0,start)+"<b>"+mid+"</b>"+ text.substring(end));
});