JavaScript - Simple Foot To Yard Conversion

Submitted by: 
Language: 
Visitors have accessed this post 120 times.

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="<a href="https://sourcecodester.com">Sourcecodester</a>
  10. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  11. </a> </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6">
  15. <h3 class="text-primary">JavaScript - Simple Foot To Yard Conversion</h3>
  16. <hr style="border-top:1px dotted #ccc;" />
  17. <div class="col-md-6">
  18. <div class="alert alert-success">Foot to Yard</div>
  19. <div style="border:1px solid #000; padding:20px; border-radius:10px;">
  20. <div class="form-group">
  21. <input type="number" id="foot1" placeholder="Enter number here" class="form-control"/>
  22. </div>
  23. <div class="form-group">
  24. <input type="text" id="yard1" class="form-control" readonly="readonly"/>
  25. </div>
  26. <center><button class="btn btn-success" onclick="convertToYard();">Convert</button></center>
  27. </div>
  28. </div>
  29. <div class="col-md-6">
  30. <div class="alert alert-success">Yard to Foot</div>
  31. <div style="border:1px solid #000; padding:20px; border-radius:10px;">
  32. <div class="form-group">
  33. <input type="number" id="yard2" placeholder="Enter number here" class="form-control"/>
  34. </div>
  35. <div class="form-group">
  36. <input type="text" id="foot2" class="form-control" readonly="readonly"/>
  37. </div>
  38. <center><button class="btn btn-success" onclick="convertToFoot();">Convert</button></center>
  39. </div>
  40. </div>
  41. </div>
  42. <script src="js/script.js"></script>
  43. </body>
  44. </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

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.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.