Categories
JavaScript Answers

How to disable click with JavaScript?

Spread the love

Sometimes, we want to disable click with JavaScript.

In this article, we’ll look at how to disable click with JavaScript.

How to disable click with JavaScript?

To disable click with JavaScript, we set the onclick property to a function that returns false.

For instance, we write

document.getElementById("myElement").onclick = () => {
  return false;
};

to select the element with getElementById.

Then we set its onclick property to a function that returns false.

The function is called when we click on the element.

And it returns false to stop the default click action.

Conclusion

To disable click with JavaScript, we set the onclick 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 *