Sometimes, we want to disable text selection while pressing shift with JavaScript.
In this article, we’ll look at how to disable text selection while pressing shift with JavaScript.
Disable Text Selection While Pressing Shift with JavaScript
To disable text selection while pressing shift with JavaScript, we can set the document.onselectstart
property to a function that returns false
.
To do this, we write:
window.onload = () => {
document.onselectstart = () => {
return false;
}
}
to set the document.onselectstart
property to a function that returns false
.
Now the user won’t be able to select anything on the screen.
We put the code in the window.onload
method to make sure that the DOM is loading before we set onselectstart
.
Conclusion
To disable text selection while pressing shift with JavaScript, we can set the document.onselectstart
property to a function that returns false
.