Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I would have done things a bit differently, but to answer your question and just get your code working I did the following:</p> <p>Here is your reworked code:</p> <pre><code>&lt;html&gt; &lt;script&gt; function calc(form) { var D = "0"; var A = document.getElementById("num1").value; var B = document.getElementById("op").value; var C = document.getElementById("num2").value; if (B === "+") { D = parseInt(A)+parseInt(C); } else if(B === "-") { D = parseInt(A)-parseInt(C); } else if(B === "*") { D = parseInt(A)*parseInt(C); } else if (B === "/") { D = parseInt(A)/parseInt(C); } document.getElementById("result").innerHTML = D; return false; } &lt;/script&gt; &lt;body&gt; &lt;input type="text" id="num1" name="num1" /&gt; &lt;input type="text" id="op" name="op" /&gt; &lt;input type="text" id="num2" name="num2" /&gt; &lt;br /&gt; &lt;input type="button" value="Solve" onClick="calc(this)"&gt; &lt;p id="result" name="r1"&gt; &lt;br /&gt; &lt;/p&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>I used the parseint() because your expressions in your if statements were treating values like text.</p> <p>Next we need to use === Three equals which says A is really equal to + or what ever the second input value is.</p> <p>Third was the onclick, I did a (this) and feed back form as you can see in the line that says function calc. </p> <p>For good measure I added a return false; to prevent form submission (but it will function without it).</p> <p>Also like other posters stated it is else if and not elseif.</p> <p>I hope this is helpful. Again, I would do things differently but got it working with some explanations.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload