JavaScript - Dynamically Solve Area Of Trapezoid

In this tutorial we will create a Dynamically Solve Area Of Trapezoid using JavaScript. This code will dynamically calculate the area of the trapezoid when the user input the needed value in the form. This code use onclick function to initiate a formula that compute the problem in order to get the area of trapezoid. This program is a user-friendly feel free to use it in your system. We will use JavaScript to allow you to implement complex things on web pages. It enables you to create dynamically updating content and add some interactive visual for the user transactions.

Getting Started:

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. <!DOCTYPE html>
  2. <html lang="en">
  3. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  4. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  5. </head>
  6. <nav class="navbar navbar-default">
  7. <div class="container-fluid">
  8. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  9. </div>
  10. </nav>
  11. <div class="col-md-3"></div>
  12. <div class="col-md-6 well">
  13. <h3 class="text-primary">JavaScript - Dynamically Solve Area Of Trapezoid</h3>
  14. <hr style="border-top:1px dotted #ccc;"/>
  15. <div class="col-md-4">
  16. <div class="form-group">
  17. <label>a</label>
  18. <input type="number" class="form-control" id="a"/>
  19. </div>
  20. <div class="form-group">
  21. <label>b</label>
  22. <input type="number" class="form-control" id="b" />
  23. </div><div class="form-group">
  24. <label>h</label>
  25. <input type="number" class="form-control" id="h" />
  26. </div>
  27. <center><button type="button" class="btn btn-primary" id="btn" onclick="calculateArea(this);">Calculate</button> <button type="button" class="btn btn-success" onclick="reset();">Reset</button></center>
  28. </div>
  29. <div class="col-md-8">
  30. <br />
  31. <div id="result"></div>
  32. </div>
  33. </div>
  34.  
  35. <script src="js/script.js"></script>
  36. </body>
  37. </html>

Creating the Script

This code contains the script of the application. This code will compute the area of the trapezoid when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js directory
  1. function reset(){
  2. document.getElementById('a').value = "";
  3. document.getElementById('b').value = "";
  4. document.getElementById('h').value = "";
  5. document.getElementById('result').innerHTML = "";
  6. document.getElementById('btn').removeAttribute('disabled');
  7. }
  8.  
  9.  
  10. function calculateArea(btn){
  11. var a = document.getElementById('a').value;
  12. var b = document.getElementById('b').value;
  13. var h = document.getElementById('h').value;
  14.  
  15. if(a != "" && b != "" && h !=""){
  16. var par_a=parseInt(a);
  17. var par_b=parseInt(b);
  18. var par_h=parseInt(h);
  19.  
  20. var area=((par_a+par_b)/2)*par_h;
  21.  
  22. btn.setAttribute('disabled', 'disabled');
  23.  
  24. document.getElementById('result').innerHTML = "<center><label style='font-size:26px;'>The Area of Trapezoid is</label> <br /><span class='text-primary' style='font-size:30px;'>"+area+"</span></center>";
  25.  
  26. }else{
  27. alert("Please enter something first!");
  28. }
  29.  
  30. }
There you have it we successfully created a Dynamically Solve Area Of Trapezoid 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