Quickly create a dropdown of world currencies on your HTML form

The Code

You can use the following utility JavaScript function that will list all currencies in a dropdown that can be placed on your HTML page.

Note: This requires JQuery libraries to be imported on your page.

/* 
 HTML element is the id of the <SELECT> tag on the HTML page.
*/
function listCurrencies(HTMLelement) {
 $.ajax({
   type : "GET",
   url : "http://restcountries.eu/rest/",
   success: function(curr) {
     var options = "";
     for(var i=0; i<curr.length; i++) {
       options += "<option value='"+curr[i].currency+"'>"+curr[i].currency+" ["+curr[i].name+"]</option>";
     }
     $('#'+HTMLelement).append(options);
   }
 }); // ajax - get currencies
} // listCurrencies()

 

The HTML code would look something like this.

< script src='https://code.jquery.com/jquery-1.12.1.min.js'></script>
<select id="currencies"></select>
< script>
    listCurrencies("currencies");
</script>

 

 

 


 

References:

  • Programmable web for the API
  • Open Exchange Rates¬†contains¬†alternative API’s (need signup). However, use of their API will need changing the code in the listCurrencies() function
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s