I have a problem with my very simple calculator in JS. I want it to show the result in a box but I don't know what it is that I'm doing wrong. There is probably some more things in my code that might not be right... Please help me!
Javascript:
document.getElementById('submit').onclick=function() {
num1 = parseFloat(document.getElementById('valueA').value);
num2 = parseFloat(document.getElementById('valueB').value);
arithmeticOperator = document.getElementById('arithmeticOperator').value;
switch(arithmeticOperator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if(num2 != 0)
{
result = num1 / num2;
}else {
result = 'Dela inte med 0!';
}
break;
default:
result = 'Error';
}
document.getElementById('calcForm').onclick=output;
window.alert('result');
};
<!DOCTYPE html>
<html>
<head>
<title>blabla</title>
<meta charset="utf-8"/>
<script src="js.js" defer></script>
</head>
<body>
<form name="calcForm" id="calcForm">
<p><input type="number" name="valueA" placeholder="Värde A" /></p>
<p><input type="number" name="valueB" placeholder="Värde B" /></p>
<select name="arithmeticOperator">
<option value="+">Addition</option>
<option value="-">Subtraktion</option>
<option value="/">Division</option>
<option value="*">Multiplication</option>
</select>
<button type="submit">Räkna ut</button>
<p><output name="result" form="calcForm"></output></p>
</form>
</body>
</html>
:)
document.getElementByIdis used with element ids, look how you using it.window.alert('result');will not display the exact result, as your calling that in single quote.