I'm making a calculator with vanilla JS and after running the switch statement to do a different thing on every case the program only returns the first case.
let res = document.getElementById("res")
let sub = document.getElementById("submit")
let sel = document.getElementById("sel")
let opt = sel.options[sel.selectedIndex];
sub.addEventListener("click", () => {
let inp1 = Number(document.getElementById("input1").value)
let inp2 = Number(document.getElementById("input2").value)
switch (opt.value) {
case "+":
res.innerHTML = inp1 + inp2;
break;
case "-":
res.innerHTML = inp1 - inp2;
break;
case "*":
res.innerHTML = inp1 * inp2;
break;
case "/":
res.innerHTML = inp1 / inp2;
break;
default:
res.innerHTML = "Please enter something in all of the empty fields";
}
})
<div>
<input id="input1" class="input w3-round">
<select class="dropdown w3-animate-zoom w3-round" id="sel">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input id="input2" class="input w3-round">
<button id="submit" class="w3-button-small w3-round w3-green">Submit</button>
</div>
<p id="res" class="p"></p>