Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable click event on text selection

I have the following jQuery, when a user clicks on a td, it reads the text of the td and then does a redirect. How can I disable the click event if the user is selecting the text instead clicking on the td?

$("td").click(function() {
    var brand = $(this).closest("tr").attr("data-brand");
    var url = window.btoa(window.location.toString());
    window.location = "?page=sku&action=brand&brand=" + brand + "&b=" + url;
});
like image 346
Get Off My Lawn Avatar asked Jul 22 '13 17:07

Get Off My Lawn


People also ask

How do you stop highlighting when clicking CSS?

To disable text selection highlighting in Google Chrome browser using CSS just set -user-select CSS property to none. And no prefix is required for Google Chrome and Opera Browsers.

How do I turn off double click highlighting?

To disable the text selection when the user double clicks on the HTML element, you can use the user-select property and set its value to none in CSS.


1 Answers

Here we go, I was able to figure it out using a function found here to get the page's selected text, if no selection was found follow link otherwise do nothing.

$("td").click(function() {
    var sel = getSelected().toString();
    if (sel === "") {
        var brand = $(this).closest("tr").attr("data-brand");
        var url = window.btoa(window.location.toString());
        window.location = "?page=sku&action=brand&brand=" + brand + "&b=" + url;
    }
});

function getSelected() {
    if (window.getSelection) {
        return window.getSelection().toString();
    } else if (document.getSelection) {
        return document.getSelection().toString();
    } else {
        var selection = document.selection && document.selection.createRange();
        if (selection.text) {
            return selection.text.toString();
        }
        return "";
    }
    return "";
}
like image 159
Get Off My Lawn Avatar answered Sep 30 '22 18:09

Get Off My Lawn