JavaScript Answers

How to Disable Clicking Inside a Div with CSS or JavaScript?

Spread the love

To disable clicking inside a div with CSS or JavaScript, we can set the pointer-events CSS property to none .

Also, we can add a click event listener to the div and then call event.preventDefault inside.

For instance, if we have the following div:

<div id='foo'>  

Then we can disable clicks inside it with CSS by writing:

#foo {  
  pointer-events: none;  

To disable clicks with JavaScript, we can write:

const foo = document.querySelector('#foo')  
foo.addEventListener('click', (event) => {  

to select the div by its ID with document.querySelector .

Then we call addEventListener on it with 'click' and a click event handler that calls event.preventDefault to stop the default action when the div is clicked.

By John Au-Yeung

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

One reply on “How to Disable Clicking Inside a Div with CSS or JavaScript?”

Leave a Reply

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