Categories
JavaScript Answers

How to Disable Text Selection While Pressing Shift with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *