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();
I 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. How can in do this.Please help.
Use the textContent property to get the text of a div element, e.g. const result = element. textContent . The textContent property will return the text content of the div and its descendants.
text) { return selection. text; } return false; } return false; }; $("#content-create-partial"). bind("mouseup", function(){ var text = getSelected(); if(text) { console. log(text); } else{ console.
To get the selected HTML as a string, you can use the following function:
function getSelectionHtml() {
    var html = "";
    if (typeof window.getSelection != "undefined") {
        var sel = window.getSelection();
        if (sel.rangeCount) {
            var container = document.createElement("div");
            for (var i = 0, len = sel.rangeCount; i < len; ++i) {
                container.appendChild(sel.getRangeAt(i).cloneContents());
            }
            html = container.innerHTML;
        }
    } else if (typeof document.selection != "undefined") {
        if (document.selection.type == "Text") {
            html = document.selection.createRange().htmlText;
        }
    }
    return html;
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With