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;
});
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.
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.
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 "";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With