Skip to content Skip to sidebar Skip to footer

Javascript Code: Dynamically Change Currencies With Dropdown Html

I have been looking for this all day - Change currencies throughout the page when choosing between countries or currencies from dropdown menu. What I basically need is a dropdown m

Solution 1:

The beauty of webapps is that you can borrow good ideas by looking a the source code (using a toll like the Firebug plugin in FF). As you can see in the example you mention the page is reloaded when a different currency is chosen:

$('#Items, #Items_input').change(function(){
    $.post('/conlogic/ajax.php?action=currency',
        {'curr': $(this).val()},
        function(data){
            if ( data=="OK" ) window.location.reload();
        });
});

Apparently in this case the page is re-rendered server side with the different currency.

Solution 2:

I write a javascript version. no Ajax, currency change rates was borrowed from google.

HTML Code

<selectid="currencySelector"><optionvalue="usd">USD</option><optionvalue="aud">AUD</option><optionvalue="eur">EUR</option><optionvalue="gbp">GBP</option></select><divclass="currency"data-currencyName="usd">15<span>USD</span></div><divclass="currency"data-currencyName="eur">15<span>EUR</span></div><divclass="currency"data-currencyName="gbp">15<span>BGP</span></div><divclass="currency"data-currencyName="aud">15<span>AUD</span></div>

Javascript Code

var 
    selector = document.getElementById("currencySelector");
var
    currencyElements = document.getElementsByClassName("currency");
var 
    usdChangeRate = {
      AUD: 1.0490, // 1AUD = 1.0490 USDEUR: 1.4407, // 1EUR = 1.4407 USDGBP: 1.6424,
      USD: 1.0
    };

selector.onchange = function () {
    var 
        toCurrency = selector.value.toUpperCase();

    for (var i=0,l=currencyElements.length; i<l; ++i) {
        var 
            el = currencyElements[i];
        var 
            fromCurrency = el.getAttribute("data-currencyName").toUpperCase();

      if (fromCurrency in usdChangeRate) {
          var// currency change to usd
              fromCurrencyToUsdAmount = parseFloat(el.innerHTML) * usdChangeRate[fromCurrency];
          var// change to currency unit selected
              toCurrenyAmount = fromCurrencyToUsdAmount / usdChangeRate[toCurrency];

          el.innerHTML = toCurrenyAmount + "<span>" + toCurrency.toUpperCase() + "</span>";
          el.setAttribute("data-currencyName",toCurrency);
      }
    }
};

Run the code

You can run the code above at http://jsbin.com/ewuyoq/5 or build your own version http://jsbin.com/ewuyoq/5/edit

Solution 3:

I would use jQuery, so feel free to disregard my answer if you don't want to use an external library. It can be found on www.jquery.com.

First, you make a span for all places where currency should be changed, give it class "currency" and in the name attribute, you put the value in your "base currency". Example:

<spanclass="currency"name="499"> 499 </span>

Then you can make a button, say it has id "showInEuro".

<inputtype="button"id="showInEuro" />

Then write some jQuery code similar to this:

var usdToEuroExchRate = 1.5; // Obviously just a magic constant// When the button is clicked
$("#showInEuro").click(function() {
     // Iterate through all of the currency spans
    $("span.currency").each(function(index) {
        // And take their names times the exchangerate and put it in the span.
        $(this).text($(this).attr("name") * usdToEuroExchRate);
    });
});

Of course, you should try to use real, live exchange rates.

I made a JSFiddle for you: http://jsfiddle.net/An3v9/9/

Post a Comment for "Javascript Code: Dynamically Change Currencies With Dropdown Html"