Skip to content

Instantly share code, notes, and snippets.

@codeyourwayup
Created October 27, 2020 08:58
Show Gist options
  • Save codeyourwayup/488be8375ea35b38037563a014bfa444 to your computer and use it in GitHub Desktop.
Save codeyourwayup/488be8375ea35b38037563a014bfa444 to your computer and use it in GitHub Desktop.
<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