Possible Duplicate:
Is there a way to make a DIV unselectable?
I have seen a number of solutions out there that work for an element. However I have an area with labels and buttons. It's not just one element it's every element within a DIV
.
How can I make anything contained within that DIV
unselectable? Note that I cannot just put a mask layer over the DIV
as the DIV
has buttons I need to be able to click.
$(".yourdiv").children().css({userSelect: 'none'});
That's in case you want to disable selection using the CSS user-select
property. If not, the above can be easily generalized to other methods.
The above only selects direct children, to select all descendants, use the .find()
method:
$(".yourdiv").find("*").css({userSelect: 'none'});
You can also do this using pure CSS:
.yourdiv * { /*this selects all elements that are children of yourdiv*/
/*user-select: none rules*/
}
Or:
.yourdiv > * { /*this selects all elements that are direct descendants of yourdiv*/
/*user-select: none rules*/
}
CSS:
div.unselectable {
-moz-user-select: -moz-none;
-khtml-user-select: none;
-webkit-user-select: none;
-o-user-select: none;
user-select: none;
}
HTML:
<div id="foo" class="unselectable">...</div>
Reference:
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