Get selected text's html in div
javascript, jquery
Solution
Select text and store it in variable called `mytext`.
if (!window.x) {
x = {};
}
x.Selector = {};
x.Selector.getSelected = function() {
var t = '';
if (window.getSelection) {
t = window.getSelection();
} else if (document.getSelection) {
t = document.getSelection();
} else if (document.selection) {
t = document.selection.createRange().text;
}
return t;
}
$(function() {
$(document).bind("mouseup", function() {
var mytext = x.Selector.getSelected();
alert(mytext);
});
});
Check working example at http://jsfiddle.net/YstZn/1/
Problem
I have a div with contentEditable set to true. I have to find selected text html. I am able to get selected text in Firefox by: ``` window.getSelection(); ``` In case of IE, I am able to get selected text html by using: ``` document.selection.createRange() ``` But, how can I find selected text html in Firefox?