PROGRAMMING | INNOVATIONS | EDUCATION | TECH NEWS

Monday, June 25, 2018

HOW TO CREATE A CALCULATOR USING HTML, CSS and JS

For many that wonders what HTML, CSS and Javascript can do, here is a little calculator you can build using the languages. These languages are not only made for web applications, you can lay your hands on building a simple calculator with it, even to an advanced one.

Look at the code below.


  1. <html>
  2. <head>
  3. <title>Calculator</title>
  4. <style>
  5. #text1,#text2{
  6.  
  7. }
  8. button{
  9. width:100%;
  10. height:100%;
  11. }
  12. </style>
  13. <script>
  14. function event1(x){
  15. document.getElementById("text1").value+=x;
  16. }
  17. function event2(){
  18. var x = document.getElementById("text1").value;
  19. document.getElementById("text1").value = eval(x);
  20. }
  21. </script>
  22.  
  23. </head>
  24. <body>
  25. <center>
  26. <table border="1">
  27. <tr>
  28. <th colspan=4><h1>Calculator</h1></th>
  29. </tr>
  30. <tr >
  31. <td colspan=4><input type="text" id="text1" placeholder="0" style="text-align:right;"/></td>
  32. </tr>
  33. <tr>
  34. <td></td>
  35. <td><button type="button" value="/" onClick="event1(this.value)">/</button></td>
  36. <td><button type="button" value="*" onClick="event1(this.value)">*</button></td>
  37. <td><button type="button" value="-" onClick="event1(this.value)">-</button></td>
  38. </tr>
  39. <tr>
  40. <td><button type="button" value="7" onClick="event1(this.value)">7</button></td>
  41. <td><button type="button" value="8" onClick="event1(this.value)">8</button></td>
  42. <td><button type="button" value="9" onClick="event1(this.value)">9</button></td>
  43. <td rowspan=2><button type="button" value="+" style="height:50px;" onClick="event1(this.value)">+</td>
  44. </tr>
  45. <tr>
  46. <td><button type="button" value="4" onClick="event1(this.value)">4</button></td>
  47. <td><button type="button" value="5" onClick="event1(this.value)">5</button></td>
  48. <td><button type="button" value="6" onClick="event1(this.value)">6</button></td>
  49. </tr>
  50. <tr>
  51. <td><button type="button" value="1" onClick="event1(this.value)">1</button></td>
  52. <td><button type="button" value="2" onClick="event1(this.value)">2</button></td>
  53. <td><button type="button" value="3" onClick="event1(this.value)">3</button></td>
  54. <td rowspan=2><button type="button" style="height:48px;" onClick="event2()" >=</button></td>
  55. </tr>
  56. <tr>
  57. <td colspan=2><button type="button" style="width:100%">0</button></td>
  58. <td><button type="button">.</button></td>
  59. </tr>
  60. </table>
  61. </center>
  62. </body>
  63. </html>

WHAT TO DO?


1. Just copy the code above into a file in any of your text editor like Notepad, Notepad++ etc.

2. save as calculator.html

3. Open in any browser of your choice..

Its as simple as that...

Watch out for more applications on HTML, CSS and JS on this platform... 

Feel free to share to any one that needs it.



No comments:

Post a Comment

Popular Posts