Blog @ RohitRox

tips tricks tuts and everything learned

Get Selected Text From Web Page

| Comments

This is a quick tip you can use for getting the selected text from your webpage.

1
2
3
4
<p>I am not selectable. You can test.</p>
<div id="here">
    How about selecting me. Try me.
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$(document).ready(function() {

    function getSelectedText(){
        var text = "";
        if (window.getSelection) {
            text = window.getSelection();
        } else if (document.getSelection) {
            text = document.getSelection();
        } else if (document.selection) {
            text = document.selection.createRange().text;
        }
        return text;
    }

    $('#here').bind("mouseup", function(){
        alert(getSelectedText());
    });

});

Comments