JavaScript - Simple Money Currency Converter

In this tutorial we will create a Simple Money Currency Converter using JavaScript. The code can convert any currency when the user input some amount and click the convert button. The code use onclick() to initiate a switch function in order to convert the given amount base on the selected currency. This is a user-friendly program feel free to modify it. We will use JavaScript to add some new feature to the website interface by actually written into an HTML page. It is what gives your page a different interactive elements and animation that engage a user.

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. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  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 Money Currency Converter</h3>
  16. <hr style="border-top:1px dotted #ccc;">
  17. <div class="col-md-3"></div>
  18. <div class="col-md-6">
  19. <div class="form-group">
  20. <label>Enter an amount</label>
  21. <select class="form-control" id="currency" style="width:30%;">
  22. <option value="Dollar">Dollar</option>
  23. <option value="Pound">Pound</option>
  24. <option value="Euro">Euro</option>
  25. <option value="Yen">Yen</option>
  26. <option value="Yuan">Yuan</option>
  27. </select>
  28. <br />
  29. <input type="number" class="form-control" id="amount"/>
  30. </div>
  31. <center><button class="btn btn-primary" onclick="convert();">Convert</button></center>
  32. <br />
  33. <div class="form-group">
  34. <label>Philippine Peso</label>
  35. <input type="number" class="form-control" id="result" readonly="readonly"/>
  36. </div>
  37. </div>
  38. </div>
  39. </body>
  40. <script src="js/script.js"></script>
  41. </html>

Creating the Script

This code contains the script of the application. This code will convert the currency 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 convert(){
  2. var amount=document.getElementById("amount").value;
  3. var currency=document.getElementById("currency").value;
  4. if(amount == ""){
  5. alert('Please enter something first');
  6. }else{
  7. switch(currency){
  8. case "Dollar":
  9. document.getElementById("result").value = amount * 51.89;
  10. break;
  11.  
  12. case "Pound":
  13. document.getElementById("result").value = amount * 72.39;
  14. break;
  15.  
  16. case "Euro":
  17. document.getElementById("result").value = amount * 63.84;
  18. break;
  19.  
  20. case "Yen":
  21. document.getElementById("result").value = amount * 0.49;
  22. break;
  23.  
  24. case "Yuan":
  25. document.getElementById("result").value = x * 8.20;
  26. break;
  27. }
  28. }
  29. }
There you have it we successfully created a Simple Money Currency Converter 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