javascript code to not select text on a web page
Mark
You can use CSS to prevent text selection on a web page by using the user-select
property. Here is an example CSS code:
body {
-webkit-user-select: none; /* Safari */
-moz-user-select: none; /* Firefox */
-ms-user-select: none; /* Internet Explorer/Edge */
user-select: none; /* Non-prefixed version */
}
This will prevent users from selecting text on the entire web page. If you want to allow text selection on certain elements, you can selectively enable text selection by overriding the user-select
property for those elements:
.allow-select {
-webkit-user-select: text; /* Safari */
-moz-user-select: text; /* Firefox */
-ms-user-select: text; /* Internet Explorer/Edge */
user-select: text; /* Non-prefixed version */
}
You can apply this class to any HTML element (such as a div
or span
) that you want to allow text selection on. Note that this approach only prevents user selection through clicking and dragging the mouse. Users can still select text through other means, such as using keyboard shortcuts or the browser's built-in selection tools.