Advertisement
richarduie

simpleCalculator2.html

Apr 2nd, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. <html>
  2.  
  3. <head>
  4. <script type="text/JavaScript">
  5. function get(eid) {return document.getElementById(eid);};
  6. function calculate(op){
  7. var a = parseFloat(get('a').value);
  8. var b = parseFloat(get('b').value);
  9. if (isNaN(a) || isNaN(b)) {
  10. alert('both inputs must be numbers');
  11. return;
  12. }
  13. var answer = a + op + b + ' = ' + eval('a' + op + 'b');
  14. get('resultAsTextBox').value = answer;
  15. }
  16. </script>
  17. </head>
  18.  
  19. <body>
  20. <form>
  21. <p>
  22. Instructions:<br />
  23. Type two numbers and click a button.<br />
  24. The answer will then appear below.<br /><br />
  25. Input values:<br/ >
  26. First number to include: = <input id="a" type="text" /><br />
  27. Second number to include: = <input id="b" type="text" /><br /><br />
  28. <input type="button" value=" + " onclick="calculate(this.value)" />
  29. <input type="button" value=" - " onclick="calculate(this.value )" />
  30. <input type="button" value=" * " onclick="calculate(this.value)" />
  31. <input type="button" value=" / " onclick="calculate(this.value)" /><br />
  32. Output value:<br />
  33. Result= <input type="text" id="resultAsTextBox" size="50" disabled="disabled" />
  34. <p>
  35. </form>
  36. </body>
  37.  
  38. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement