JavaScript - Get Area of Triangle

In this tutorial we will create a Get Area of Triangle using JavaScript. This code will automatically calculate the area of the triangle when the user provide the needed value in the form inputs. This code use onclick function to initiate an algorithm to compute the problem in order to get the area of the triangle . 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 - Get Area of Triangle</h3>
  14. <hr style="border-top:1px dotted #ccc;"/>
  15. <div class="col-md-3"></div>
  16. <div class="col-md-6">
  17. <center><h2>Area of Triangle</h2></center>
  18. <br />
  19. <div class="form-group">
  20. <label>Height</label>
  21. <input type="number" class="form-control" id="height" required="required"/>
  22. </div>
  23. <div class="form-group">
  24. <label>Width</label>
  25. <input type="number" class="form-control" id="width" required="required"/>
  26. </div>
  27. <center><button type="button" class="btn btn-primary" onclick="calculateArea();">Calculate</button> <button type="button" class="btn btn-success" onclick="reset();">Reset</button></center>
  28.  
  29. <br />
  30. <div id="result"></div>
  31. </div>
  32. </div>
  33.  
  34. <script src="js/script.js"></script>
  35. </body>
  36. </html>

Creating the Script

This code contains the script of the application. This code will compute the area of the triangle 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 calculateArea(){
  2. var height = document.getElementById('height').value;
  3. var width = document.getElementById('width').value;
  4.  
  5. var area = (width * height) / 2;
  6.  
  7. document.getElementById('height').setAttribute('readonly', 'readonly');
  8. document.getElementById('width').setAttribute('readonly', 'readonly');
  9. document.getElementById('result').innerHTML = "<center><label style='font-size:26px;'>The area of triangle is</label> <br /><span class='text-primary' style='font-size:30px;'>"+area+"</span></center>";
  10.  
  11. }
  12.  
  13.  
  14. function reset(){
  15. document.getElementById('height').value = "";
  16. document.getElementById('width').value = "";
  17. document.getElementById('height').removeAttribute('readonly');
  18. document.getElementById('width').removeAttribute('readonly');
  19. document.getElementById('result').innerHTML = "";
  20. }
There you have it we successfully created a Get Area of Triangle 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