Basically I am trying to get selected text if a user click on a div.
Code:
$('.boldTextButton').mouseup(function(event){
var SelectionDetails = getSelectionText();
console.log(SelectionDetails);
});
function getSelectionText() {
var text = "", startRange=0, endRange=0;
if (window.getSelection) {
text = window.getSelection().toString();
startRange = window.getSelection().anchorOffset;
endRange = window.getSelection().focusOffset;
} else if (document.selection && document.selection.type != "Control"){
text = document.selection.createRange().text;
}
return [text, endRange, startRange];
}
Unfortunately with above method, when the mouse is clicked, it first de-select the text and then the returned value I get is empty ""
.
I also have tried event.preventDefault()
and event.stopPropegation()
, still didn't work.