JavaScript - Get Area of Triangle
Submitted by razormist on Saturday, April 13, 2019 - 18:32.
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.
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!
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.- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- </div>
- </nav>
- <div class="col-md-6 well">
- <hr style="border-top:1px dotted #ccc;"/>
- <div class="col-md-6">
- <br />
- <div class="form-group">
- <input type="number" class="form-control" id="height" required="required"/>
- </div>
- <div class="form-group">
- <input type="number" class="form-control" id="width" required="required"/>
- </div>
- <br />
- </div>
- </div>
- </body>
- </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- function calculateArea(){
- var height = document.getElementById('height').value;
- var width = document.getElementById('width').value;
- var area = (width * height) / 2;
- document.getElementById('height').setAttribute('readonly', 'readonly');
- document.getElementById('width').setAttribute('readonly', 'readonly');
- 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>";
- }
- function reset(){
- document.getElementById('height').value = "";
- document.getElementById('width').value = "";
- document.getElementById('height').removeAttribute('readonly');
- document.getElementById('width').removeAttribute('readonly');
- document.getElementById('result').innerHTML = "";
- }
Add new comment
- 324 views