JavaScript - Simple Foot To Yard Conversion

In this tutorial we will create a Simple Foot To Yard Conversion using JavaScript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is widely used in designing a stunning website. It is an interpreted programming language that has a capabilities of Object-Oriented. So Let's do the 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.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7.         <nav class="navbar navbar-default">
  8.                 <div class="container-fluid">
  9.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10.                 </div>
  11.         </nav>
  12.         <div class="col-md-3"></div>
  13.         <div class="col-md-6">
  14.                 <h3 class="text-primary">JavaScript - Simple Foot To Yard Conversion</h3>
  15.                 <hr style="border-top:1px dotted #ccc;" />
  16.                 <div class="col-md-6"> 
  17.                         <div class="alert alert-success">Foot to Yard</div>
  18.                         <div style="border:1px solid #000; padding:20px; border-radius:10px;">
  19.                                 <div class="form-group">
  20.                                         <input type="number" id="foot1" placeholder="Enter number here" class="form-control"/>
  21.                                 </div>
  22.                                 <div class="form-group">
  23.                                         <input type="text" id="yard1" class="form-control" readonly="readonly"/>
  24.                                 </div>
  25.                                 <center><button class="btn btn-success" onclick="convertToYard();">Convert</button></center>
  26.                         </div>
  27.                 </div>
  28.                 <div class="col-md-6">
  29.                         <div class="alert alert-success">Yard to Foot</div>
  30.                         <div style="border:1px solid #000; padding:20px; border-radius:10px;">
  31.                                 <div class="form-group">
  32.                                         <input type="number" id="yard2" placeholder="Enter number here" class="form-control"/>
  33.                                 </div>
  34.                                 <div class="form-group">
  35.                                         <input type="text" id="foot2" class="form-control" readonly="readonly"/>
  36.                                 </div>
  37.                                 <center><button class="btn btn-success" onclick="convertToFoot();">Convert</button></center>
  38.                         </div>
  39.                 </div>
  40.         </div>
  41. <script src="js/script.js"></script>
  42. </body>
  43. </html>

Creating the Script

This code contains the script of the application. This code will automatically convert the foot to yard value or vice versa. 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 folder.
  1. function convertToYard(){
  2.         var foot1 = document.getElementById("foot1").value;
  3.         if(foot1 != ""){
  4.                 var number = parseFloat(foot1);
  5.                 document.getElementById("yard1").value = (number*0.333333)+"yd";
  6.         }else{
  7.                 alert("Please enter something");
  8.         }
  9. }
  10.  
  11. function convertToFoot(){
  12.         var yard2 = document.getElementById("yard2").value;
  13.         if(yard2 != ""){
  14.                 var number = parseFloat(yard2);
  15.                 document.getElementById("foot2").value = (number*3)+"ft";
  16.         }else{
  17.                 alert("Please enter something");
  18.         }
  19. }
  20. </javascrip>
  21. There you have it we successfully created a <strong>Simple Foot To Yard Conversion</strong> 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