Last updated: Apr 4, 2024
Reading time·4 min
If you need to disable text selection on double-click using CSS, set the
element's user-select
CSS property to none
.
When the user-select
property is set to none
, the text of the element and
its sub-elements is not selectable.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <style> .text { user-select: none; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; -khtml-user-select: none; } </style> </head> <body> <h2 class="text">bobbyhadz.com</h2> <p class="text">Website: bobbyhadz.com</p> </body> </html>
We set the class
attribute on the two elements to text
and used the class to
set the
user-select CSS
property to none
.
user-select
property controls whether the user can select the element's text.When the property is set to none
, the text of the element and its sub-elements
is not selectable.
You can also set user-select
to none
on multiple elements by wrapping them
with a div
tag.
<div style="user-select: none;"> <h2>bobbyhadz.com</h2> <p>Website: bobbyhadz.com</p> </div>
The example uses an inline style tag, however, you can also use an external
.css
file.
Here is the updated index.html
.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <link rel="stylesheet" href="style.css" /> </head> <body> <h2 class="text">bobbyhadz.com</h2> <p class="text">Website: bobbyhadz.com</p> </body> </html>
And here is the code for the style.css
file.
.text { user-select: none; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; -khtml-user-select: none; }
The CSS properties set user-select
to none
for all browsers.
To disable text selection on double-click using JavaScript:
dblclick
event listener on the element.Here is the HTML for the example.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <h2 class="text">bobbyhadz.com</h2> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const paragraph = document.querySelector('.text'); paragraph.addEventListener('dblclick', event => { console.log('double-click event triggered'); if (document.selection && document.selection.empty) { document.selection.empty(); } else if (window.getSelection) { const selection = window.getSelection(); selection.removeAllRanges(); } });
We used the
document.querySelector method to
select the p
element and
added a dblclick event listener to the element.
The dblclick event is triggered when the user's pointing device button is double-clicked.
In the event handler function, we check if there is a selection and clear it.
paragraph.addEventListener('dblclick', event => { console.log('double-click event triggered'); if (document.selection && document.selection.empty) { document.selection.empty(); } else if (window.getSelection) { const selection = window.getSelection(); selection.removeAllRanges(); } });
The selection.empty() and selection.removeAllRanges() methods remove all ranges from the selection.
event.detail
You can also use the event.detail
property to disable text selection on double
click.
If the property returns a value greater than 1
, then the user clicked
multiple times.
Here is the HTML for the example.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <h2 class="text">bobbyhadz.com</h2> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const paragraph = document.querySelector('.text'); paragraph.addEventListener('mousedown', event => { if (event.detail > 1) { event.preventDefault(); } });
We added a mousedown event listener to the paragraph.
The mousedown
event is triggered at an element when the user's pointing device
button is pressed while it's inside the element.
event.preventDefault()
method.The example above only disables text selection on double-click for the specific element.
If you need to disable text selection on double-click for all elements, set the
event listener on the document.body
element.
document.body.addEventListener('mousedown', event => { if (event.detail > 1) { event.preventDefault(); } });
Note that the user can still select the element's text with a single click, however, they can't select the element's text with a double-click.
If you want to prevent the user from selecting the element's text with a single
or double-click, then remove the if
statement.
const paragraph = document.querySelector('.text'); paragraph.addEventListener('mousedown', event => { event.preventDefault(); });
The example prevents the user from selecting the element's text with a single and double-click.
If you want to apply this to all elements in the document, add the mousedown
event listener to the document.body
element.
document.body.addEventListener('mousedown', event => { event.preventDefault(); });
onselectstart
You can also set the onselectstart
property to a function that returns false
straight away to disable text selection on double-click.
Here is the HTML for the example.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <h2 class="text">bobbyhadz.com</h2> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const paragraph = document.querySelector('.text'); paragraph.onselectstart = function handleSelectStart() { return false; };
You can also set the event listener for all elements in the document.
document.body.onselectstart = function handleSelectStart() { return false; };
You can also use the addEventListener()
method to add an event listener for
the
selectstart
event.
const paragraph = document.querySelector('.text'); paragraph.addEventListener('selectstart', event => { event.preventDefault(); });
The event listener can also be set on the document.body
element if you need to
disable text selection on double-click for all elements in the document.
document.body.addEventListener('selectstart', event => { event.preventDefault(); });
You can learn more about the related topics by checking out the following tutorials: