Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace usage of alert() in mouseEvent #7778

Merged
merged 2 commits into from
Aug 10, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions files/en-us/web/api/mouseevent/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -112,26 +112,25 @@ <h2 id="Example">Example</h2>

<h3 id="HTML">HTML</h3>

<pre class="brush: html">&lt;p&gt;&lt;label&gt;&lt;input type="checkbox" id="checkbox"&gt; Checked&lt;/label&gt;
&lt;p&gt;&lt;button id="button"&gt;Click me&lt;/button&gt;</pre>
<pre class="brush: html">&lt;p&gt;&lt;label&gt;&lt;input type="checkbox" id="checkbox"&gt; Checked&lt;/label&gt;&lt;/p&gt;
&lt;p&gt;&lt;button id="button"&gt;Click me&lt;/button&gt;&lt;/p&gt;
</pre>

<h3 id="JavaScript">JavaScript</h3>

<pre class="brush: js">function simulateClick() {
var evt = new MouseEvent("click", {
// 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
});
var cb = document.getElementById("checkbox"); //element to click on
var canceled = !cb.dispatchEvent(evt);
if(canceled) {
// A handler called preventDefault
alert("canceled");
} else {
// None of the handlers called preventDefault
alert("not canceled");
}

// Send the event to the checkbox element
cb.dispatchEvent(evt);
}
document.getElementById("button").addEventListener('click', simulateClick);</pre>

Expand Down