From df2a65434147bddc2ef824809246e395652b18d6 Mon Sep 17 00:00:00 2001 From: Anil Seervi Date: Tue, 10 Aug 2021 09:45:17 +0530 Subject: [PATCH 1/2] Replace usage of alert() --- files/en-us/web/api/mouseevent/index.html | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/files/en-us/web/api/mouseevent/index.html b/files/en-us/web/api/mouseevent/index.html index f6bf28f8d9c72e7..7b066ee4847e3aa 100644 --- a/files/en-us/web/api/mouseevent/index.html +++ b/files/en-us/web/api/mouseevent/index.html @@ -112,25 +112,29 @@

Example

HTML

-
<p><label><input type="checkbox" id="checkbox"> Checked</label>
-<p><button id="button">Click me</button>
+
<p><label><input type="checkbox" id="checkbox"> Checked</label></p>
+<p><button id="button">Click me</button></p>
+
+<p id="output"></p>
+

JavaScript

function simulateClick() {
-  var evt = new MouseEvent("click", {
+  let evt = new MouseEvent("click", {
     bubbles: true,
     cancelable: true,
     view: window
   });
-  var cb = document.getElementById("checkbox"); //element to click on
-  var canceled = !cb.dispatchEvent(evt);
+  const cb = document.getElementById("checkbox"); //element to click on
+  const output = document.getElementById("output"); //element to display output
+  let canceled = !cb.dispatchEvent(evt);
   if(canceled) {
     // A handler called preventDefault
-    alert("canceled");
+    output.textContent = "canceled";
   } else {
     // None of the handlers called preventDefault
-    alert("not canceled");
+    output.textContent = "not canceled";
   }
 }
 document.getElementById("button").addEventListener('click', simulateClick);
From 3c0880556bc01d99a53218d04b8f95da1d56e15d Mon Sep 17 00:00:00 2001 From: Anil Seervi Date: Tue, 10 Aug 2021 18:22:36 +0530 Subject: [PATCH 2/2] Update example --- files/en-us/web/api/mouseevent/index.html | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/files/en-us/web/api/mouseevent/index.html b/files/en-us/web/api/mouseevent/index.html index 7b066ee4847e3aa..a398a61438f1ed1 100644 --- a/files/en-us/web/api/mouseevent/index.html +++ b/files/en-us/web/api/mouseevent/index.html @@ -114,28 +114,23 @@

HTML

<p><label><input type="checkbox" id="checkbox"> Checked</label></p>
 <p><button id="button">Click me</button></p>
-
-<p id="output"></p>
 

JavaScript

function simulateClick() {
+// Get the element to send a click event
+const cb = document.getElementById("checkbox");
+
+// Create a synthetic click MouseEvent
   let evt = new MouseEvent("click", {
     bubbles: true,
     cancelable: true,
     view: window
   });
-  const cb = document.getElementById("checkbox"); //element to click on
-  const output = document.getElementById("output"); //element to display output
-  let canceled = !cb.dispatchEvent(evt);
-  if(canceled) {
-    // A handler called preventDefault
-    output.textContent = "canceled";
-  } else {
-    // None of the handlers called preventDefault
-    output.textContent = "not canceled";
-  }
+  
+// Send the event to the checkbox element
+cb.dispatchEvent(evt);
 }
 document.getElementById("button").addEventListener('click', simulateClick);