Advertisement
Guest User

Simple JavaScript Percentage Calculator

a guest
Aug 26th, 2012
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <h1>Find Percentage of a Number</h1>
  2. <ul>
  3. <li>
  4. <label for="base">Base:</label>
  5. <input type="text" id="base" />
  6. </li>
  7. <li>
  8. <label for="percent">Percent:</label>
  9. <input type="text" id="percent" />
  10. </li>
  11. <li>
  12. <p><span id="total">Total?</span></p>
  13. </li>
  14. <li>
  15. <input type="submit" id="submit" value="Calculate?" onclick="mainInit()" />
  16. </li>
  17. </ul>
  18. <script type="text/javascript">
  19.  
  20. function mainInit() {
  21.     if(document.getElementById) {
  22.         if(document.getElementById("submit").onclick) {
  23.             calculate();
  24.         }
  25.     }
  26.     else {
  27.         alert("Sorry, your browser does not support JavaScript.");
  28.     }
  29. }
  30.  
  31. function calculate() {
  32.     var a = parseFloat(document.getElementById("base").value);
  33.     var b = parseFloat(document.getElementById("percent").value);
  34.     var total = document.getElementById("total");
  35.     total.innerHTML = (a * (b/100)).toFixed(2);
  36. }
  37. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement