userSelect Property - Javascript CSS Style Property

Javascript examples for CSS Style Property:userSelect

Description

The userSelect property sets or gets whether the element's text can be selected or not.

Property Values

Parameter Description
auto Default.
none Prevent text selection
text The text can be selected by the user
all Text selection is made with one click instead of a double-click

Technical Details

Item Value
Default Value: auto
Return Value: A String, representing whether the text of an element can be selected
CSS VersionCSS3

Prevent text selection of a <div> element:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Test</button>

<p id="demo">This is a test. This is a test. This is a test.</p>

<script>
function myFunction() {//from   www . ja v a2 s.  c  o  m
    var x = document.getElementById("demo");
    x.style.WebkitUserSelect = "none"; // Chrome, Safari, Opera
    x.style.MozUserSelect = "none"; // Firefox
    x.style.msUserSelect = "none"; // IE 10+
    x.style.userSelect = "none"; // Standard syntax
}
</script>

</body>
</html>

Related Tutorials