Created
October 27, 2020 08:58
-
-
Save codeyourwayup/488be8375ea35b38037563a014bfa444 to your computer and use it in GitHub Desktop.
Saved from https://javascript.info/dispatch-events
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<button id="elem" onclick="alert('Click!');">Autoclick</button> | |
<script> | |
let event = new Event("click"); | |
elem.dispatchEvent(event); | |
</script> | |
<h1 id="elem">Hello from the script!</h1> | |
<script> | |
// catch on document... | |
document.addEventListener("hello", function(event) { // (1) | |
alert("Hello from " + event.target.tagName); // Hello from H1 | |
}); | |
// ...dispatch on elem! | |
let event = new Event("hello", {bubbles: true}); // (2) | |
elem.dispatchEvent(event); | |
// the handler on document will activate and display the message. | |
</script> | |
static async setInputValue(input, value, delay, dispatchEvent=true) { | |
if (value === undefined) return input; | |
input.type === 'select-one' ? input.value = value : await this.type(input, value, delay); | |
if (dispatchEvent) { | |
input.dispatchEvent(new Event('change')); | |
} | |
return input; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment