i created a javascript calculator and i used variables for everything but it won't work.
I tried to use document.getElementById("box1").value istead of just v1 and i saw that something changed, i got 1111 if i type 11 + 11, its different, why?
I don't know what is the problem, i tried a few things but nothing changed.
Here is my code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JS calculator</title>
</head>
<body>
<input id="box1" type="text" placeholder="Number one" />
<select name="" id="select">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input id="box2" type="text" placeholder="Number two" />
<button onclick="count()">Check</button>
<a id="output"></a>
<script>
var v1 = document.getElementById("box1").value;
var v2 = document.getElementById("box2").value;
var sel = document.getElementById("select").value;
var out = document.getElementById("output").innerHTML;
function count() {
if (sel === "+") {
out = v1 + v2;
}
else if (sel === "-") {
out = v1 - v2;
}
else if (sel === "*") {
out = v1 * v2;
}
else if (sel === "/") {
out = v1 / v2;
}
else {
out = "Error Error Error!"
}
}
</script>
</body>
</html>
Thanks! :)