Prevent onClick event when selecting text
One option is to check the type
of the Selection
object returned by window.getSelection
:
function toggleInfo() {
var selection = window.getSelection();
if(selection.type != "Range") {
$("#clicktoshow").toggle();
$("#information").toggle();
}
}
http://jsfiddle.net/k61u66ek/4/
Update
If the browser you're targeting doesn't expose a type
property on the Selection
object then you can test against the length of the selected value instead:
function toggleInfo() {
var selection = window.getSelection();
if(selection.toString().length === 0) {
$("#clicktoshow").toggle();
$("#information").toggle();
}
}
http://jsfiddle.net/k61u66ek/9/
which can in turn be reduced down to a bool check on toString
:
if(!selection.toString()) {
http://jsfiddle.net/k61u66ek/10/
You could check if there is a selection made in the click event handler:
window.getSelection().toString();