Niraj Choubey
Niraj Choubey

Reputation: 4040

Get selected text's html in div

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?

Upvotes: 17

Views: 34584

Answers (6)

Hakan
Hakan

Reputation: 589

    $('.link').click(function(){
        var mytext = getSelected();
        alert(mytext);
    })

function getSelected() {
    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;
}

Upvotes: 0

jerblack
jerblack

Reputation: 1287

According to MDN:

"A Selection object, when cast to string, either by appending an empty string ("") or using Selection.toString(), returns the text selected."

For me, any of the following work in Chrome 86:

String(window.getSelection())
window.getSelection() + ""
window.getSelection().toString()
String(document.getSelection())
document.getSelection() + ""
document.getSelection().toString()

https://developer.mozilla.org/en-US/docs/Web/API/Window/getSelection

Upvotes: 0

Tim Down
Tim Down

Reputation: 324477

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;
}

Upvotes: 33

Hussein
Hussein

Reputation: 42808

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/

Upvotes: 18

Zirak
Zirak

Reputation: 39808

window.getSelection().getRangeAt(0);

It returns a document fragment. It contains the nodes where the selection begins and ends and some other juicy stuff. Inspect it with FireBug or another JavaScript console, &&|| for more info

Upvotes: 2

Naftali
Naftali

Reputation: 146302

to get the text of a div you do this:(in jQuery)

var text = $('div.selector').text();

Upvotes: -2

Related Questions