JavaScript - Simple Area of Rectangle Solver

Submitted by: 
Visitors have accessed this post 81 times.

In this tutorial we will create a Simple Area of Rectangle Solver using JavaScript. This code will automatically calculate the area of the rectangle 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 rectangle. 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

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="<a href="">Sourcecodester</a>
  9. " rel="nofollow">">Sourcecodester</a>
  10. </a> </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">JavaScript - Simple Area of Rectangle Solver</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-4">
  17. <div class="form-group">
  18. <label>Length</label>
  19. <input type="number" class="form-control" id="length" required="required"/>
  20. </div>
  21. <div class="form-group">
  22. <label>Height</label>
  23. <input type="number" class="form-control" id="height" required="required"/>
  24. </div>
  25. <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>
  26. </div>
  27. <div class="col-md-8">
  28. <br />
  29. <div id="result"></div>
  30. </div>
  31. </div>
  33. <script src="js/script.js"></script>
  34. </body>
  35. </html>

Creating the Script

This code contains the script of the application. This code will compute the area of the rectangle 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('height').value = "";
  3. document.getElementById('length').value = "";
  4. document.getElementById('result').innerHTML = "";
  5. document.getElementById('btn').removeAttribute('disabled');
  6. }
  9. function calculateArea(btn){
  10. var height = document.getElementById('height').value;
  11. var length = document.getElementById('length').value;
  13. var area = (length * height);
  15. btn.setAttribute('disabled', 'disabled');
  17. document.getElementById('result').innerHTML = "<center><label style='font-size:26px;'>The Area of rectangle is</label> <br /><span class='text-primary' style='font-size:30px;'>"+area+"</span></center>";
  19. }

There you have it we successfully created a Simple Area of Rectangle Solver 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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.