-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample2.html
More file actions
39 lines (37 loc) · 1.1 KB
/
Example2.html
File metadata and controls
39 lines (37 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Calculator</title>
<link rel="stylesheet" href="example2.css">
</head>
<body>
<h2>Simple Calculator</h2>
<input type="text" id="klu">
<button onclick="calculate()" id="valueBtn">=</button>
<button onclick="clearDisplay()">Clear</button>
<h1 class="answer" id="answer"></h1>
<script>
const inputEl = document.getElementById("klu");
const answerEl = document.getElementById("answer");
inputEl.addEventListener("keydown", (event) =>{
if (event.key === "Enter") {
calculate();
}
});
function calculate() {
try {
const expr = inputEl.value;
answerEl.textContent = "Result: " + eval(expr);
} catch {
alert("Error");
}
}
function clearDisplay() {
inputEl.value = "";
answerEl.textContent = "";
}
</script>
</body>
</html>