Show user menu when selecting text

Hi, I want to be able to display a custom menu (or context menu) when the user selects some text that has a lot in common with what the environment provides.

context menu

How to implement this something like this? I know about native / jquery context menu plugins, but how do I know when a user selects text? The onselect browser seems to be supported only on input elements.

+4
source share
1 answer

Here is a pretty simple listener for .getSelection(): DEMO

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

$(document).ready(function() {
    $(document).bind("mouseup", function() {
        var selectedText = x.Selector.getSelected();
        if(selectedText != ''){
            alert(selectedText);
        }
    });
});

/ . , !

, / .

+9

All Articles