Disable click event when selecting text

I have the following jQuery, when the user clicks on td, he reads the text td and then redirects. How can I turn off the click event if the user selects the text, instead clicks on 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; }); 
+4
source share
1 answer

Here we go, I was able to figure it out using the function found here , to get the selected text, if no choices were found, follow the 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 ""; } 
+7
source

Source: https://habr.com/ru/post/1492752/


All Articles