Get Selected text from Web Page

|

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

    <p>I am not selectable. You can test.</p>
    <div id="here">
        How about selecting me. Try me.
    </div>
    $(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