Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make MANY elements unselectable using jQuery or CSS? [duplicate]

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.

like image 650
Samantha J T Star Avatar asked Oct 07 '12 14:10

Samantha J T Star


2 Answers

$(".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*/
}
like image 144
Chris Avatar answered Nov 14 '22 22:11

Chris


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:

  • Is there a way to make a DIV unselectable?
  • Disabling Selection in jQuery
like image 41
Koerr Avatar answered Nov 14 '22 22:11

Koerr