JavaScript cancel/clear/disable the selection of the text
By using the JavaScript we can cancel/clear the text selected by the user.
We can clear or cancel the text immediately when user select the text.
So whenever user want to select the text it will always double click on text.
Now as we know before the double click (dblclick) event mousedown click event is trigger. And the text selection is always happens on the mousedown event.
So we just have to return false value from the mousedown event of the text, so the text will be not selected.
Example 1:
<p>Hello <b onmousedown="return false;" ondblclick="alert('Text will not select.')">this text will be not select.</b></p>
Disable Text Selection Live Demo
In above example we can stop the user to not select the any text.
But we can also clear the selected text when user select the by double click on it.
For clear the selected text we can use the getSelection().removeAllRanges()
method.
So we have to call this method on the double click (dblclick) event.
<p>Hello <b ondblclick="getSelection().removeAllRanges()">this text will be not select.</b></p>
Clear Text Selection Live Demo
If you check the above live demo than you may be get the text is select first and after that it will be clear.