Skip to content

Instantly share code, notes, and snippets.

@biologyscience
Created August 3, 2022 15:56
Show Gist options
  • Save biologyscience/295be8fc18a36ed9c73bc67bcdfc36f8 to your computer and use it in GitHub Desktop.
Save biologyscience/295be8fc18a36ed9c73bc67bcdfc36f8 to your computer and use it in GitHub Desktop.
send info with events
const a = document.getElementById('a');
const aVal = parseInt(a.value);
const myEvent = new CustomEvent('myEvent', {detail: aVal});
document.dispatchEvent(myEvent);
document.addEventListener('myEvent', (data) =>
{
const b = document.getElementById('b');
const aVal = data.detail;
b.value = aVal + 5;
});
<html>
<head>
<script src="a.js" defer></script>
<script src="b.js" defer></script>
</head>
<body>
<input id="a" type="text" value="10">
<input id="b" type="text">
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment