How to disable mouse event on certain elements using JavaScript?

Following is the code for disabling mouse event on certain elements using JavaScript −


 Live Demo

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
   body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
   .result {
      font-size: 18px;
      font-weight: 500;
      color: rebeccapurple;
   .size {
      font-size: 30px;
<h1>Disable Mouse events using JavaScript</h1>
<div class="result">This is some text inside a div</div>
<button class="Btn">DISABLE</button>
<button class="Btn">ENABLE</button>
Click on the above button to enable or disable mouse events
   let resEle = document.querySelector(".result");
   let sampleEle = document.querySelector(".sample");
   let BtnEle = document.querySelectorAll(".Btn");
   resEle.addEventListener("click", () => {
   BtnEle[0].addEventListener("click", () => { = "none";
   BtnEle[1].addEventListener("click", () => { = "auto";


The above code will produce the following output −

On clicking the text in purple above −

On clicking the ‘DISABLE’ button and then clicking the text −

On clicking the ‘ENABLE’ button and then clicking on the text −

Updated on: 17-Jul-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started