Skip to main content

Checkbox Autosum Using JavaScript

Screenshot
Body

In this article we create Checkbox Autosum using JavaScript. The program will automatically calculate the total cost of an item base on the checked input boxes. The trick of this code is that you need to bind all the check boxes with unique identifiers in order to get the value and calculate the sum by using Number(). To learn more about this, just follow the steps below.

Getting started:

First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.

  1. <br />
  2. <!DOCTYPE html><br />
  3. <html lang="en"><br />
  4. <head><br />
  5. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/><br />
  6. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" /><br />
  7. </head><br />
  8. <body><br />
  9. <nav class="navbar navbar-default"><br />
  10. <div class="container-fluid"><br />
  11. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a><br />
  12. </div><br />
  13. </nav><br />
  14. <div class="col-md-3"></div><br />
  15. <div class="col-md-6 well"><br />
  16. <h3 class="text-primary">Checkbox Autosum Using JavaScript</h3><br />
  17. <hr style="border-top:1px dotted #ccc;"><br />
  18. <div class="col-md-4"><br />
  19. <center><h3 class="text-info">SHAKE STAND</h3></center><br />
  20. <div class="form-inline"><br />
  21. <div class="form-inline"><br />
  22. <label>Total Purchase</label><br />
  23. <input type="text" id="total" disabled="disabled"/><br />
  24. </div><br />
  25. </div><br />
  26. <br /><br />
  27. <div class="form-group"><br />
  28. <label><input type="checkbox" value="250" id="p1" onclick="autoSum(this);"> Milk, Price: 250</label><br />
  29. <label><input type="checkbox" value="350" id="p2" onclick="autoSum(this);"> Strawberry, Price: 350</label><br />
  30. <label><input type="checkbox" value="400" id="p3" onclick="autoSum(this);"> Mocha, Price: 400</label><br />
  31. <label><input type="checkbox" value="350" id="p4" onclick="autoSum(this);"> Watermelon, Price: 350</label><br />
  32. <label><input type="checkbox" value="350" id="p5" onclick="autoSum(this);"> Watermelon, Price: 350</label><br />
  33. <label><input type="checkbox" value="300" id="p5" onclick="autoSum(this);"> Mango, Price: 300</label><br />
  34. </div><br />
  35. </div><br />
  36. </div><br />
  37. <script src="js/script.js"></script><br />
  38. </body><br />
  39. </html><br />

Creating the Script

This code contains the script of the application. The code will auto calculate the sum of all products. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.

  1. <br />
  2. var total=0;<br />
  3. function autoSum(elem) {</p>
  4. <p> if (elem.checked == true) {<br />
  5. total += Number(elem.value);<br />
  6. }else{<br />
  7. total -=Number(elem.value);<br />
  8. }</p>
  9. <p> document.getElementById('total').value = total.toFixed(0);<br />
  10. }<br />

There you have it we successfully created a Checkbox Autosum using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment