Skip to content

Instantly share code, notes, and snippets.

@vharsh
Created February 27, 2024 00:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vharsh/d53046f06fd6ea3a6599b2e0b87b3e7c to your computer and use it in GitHub Desktop.
Save vharsh/d53046f06fd6ea3a6599b2e0b87b3e7c to your computer and use it in GitHub Desktop.
DOM playground
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculator</title>
<script>
function execute() {
console.log('some DOM event triggered this function call');
console.log('triggered via xxx');
// 1. fetch the values
let x = document.getElementById("one")?.value;
let y = document.getElementById("two")?.value;
try {
// 2. Compute the result
let a = Number(x);
let b = Number(y);
document.getElementById('result').innerHTML = a + b;
} catch(e) {
console.log("TODO: why isn't a TypeError thrown during a failed casting operation?")
console.log(e);
}
}
</script>
</head>
<body>
<input id="one" onchange="execute()"/>
<p> + </p>
<input id="two" onchange="execute()"/>
<!-- <button onclick="execute()"> Submit </button> -->
<p id="result"></p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment