Skip to main content

Javascript - Simple Currency Converter

Screenshot
Body
In this tutorial we will create a Simple Currency Converter using Javascript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It can renders web pages in an interactive and dynamic fashion. It is widely used in designing a stunning website. This code can be used as your converter to your mathematical problem. So Let's do the coding.

Before we started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And this is the link for the bootstrap that has been used for the layout of the calculator https://getbootstrap.com/

The Main Interface

This code contains the interface of the application. This code will render application and display the form that will be use in converting. 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. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  5. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  6. <script src="converter.js"></script>
  7. </head>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">Javascript - Simple Currency Converter</h3>
  16. <hr style="border-top:1px dotted #ccc;">
  17. <div class="col-md-2"></div>
  18. <div class="col-md-8">
  19. <h4>Converter</h4>
  20. <hr style="border-top:1px groovy #ccc;"/>
  21. <div class="form-group">
  22. <select onchange="Currency(); Calculate()" class="form-control" id="converter" style="width:30%;">
  23. <option value="Dollar">Dollar</option>
  24. <option value="Pound">Pound</option>
  25. <option value="Euro">Euro</option>
  26. <option value="Yen">Yen</option>
  27. <option value="Yuan">Yuan</option>
  28. </select>
  29. <br />
  30. <input type="number" oninput="Calculate()" class="form-control" id="value1"/>
  31. </div>
  32. <br /><br />
  33. <div class="form-group">
  34. <label>Philippine Peso</label>
  35. <input type="number" class="form-control" id="value2" disabled="disabled"/>
  36. </div>
  37. </div>
  38. </div>
  39. </body>
  40. </html>

Creating the Script

This code contains the script of the application. This code will convert input value every time you enter some number. To do this just copy write the code as shown below inside the text editor and save it as converter.js
  1. function Currency(){
  2. y = document.getElementById("converter").value;
  3. return y;
  4. }
  5.  
  6. function Calculate(){
  7. y = Currency();
  8.  
  9. x = document.getElementById("value1").value;
  10.  
  11. if(x == ""){
  12. document.getElementById("value2").value = "";
  13. }else{
  14. switch(y){
  15. case "Dollar":
  16. document.getElementById("value2").value = x * 51.89;
  17. break;
  18.  
  19. case "Pound":
  20. document.getElementById("value2").value = x * 72.39;
  21. break;
  22.  
  23. case "Euro":
  24. document.getElementById("value2").value = x * 63.84;
  25. break;
  26.  
  27. case "Yen":
  28. document.getElementById("value2").value = x * 0.49;
  29. break;
  30.  
  31. case "Yuan":
  32. document.getElementById("value2").value = x * 8.20;
  33. break;
  34. }
  35. }
  36. }
There you have it we successfully created a Simple Currency Converter using javascript. I hope that this very simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!

Comments

Add new comment