Javascript code: dynamically change currencies with dropdown HTML

后端 未结 3 997
小鲜肉
小鲜肉 2021-01-25 22:34

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

3条回答
  •  不知归路
    2021-01-25 23:08

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

    HTML Code

      
      
    15USD
    15EUR
    15BGP
    15AUD

    Javascript Code

    var 
        selector = document.getElementById("currencySelector");
    var
        currencyElements = document.getElementsByClassName("currency");
    var 
        usdChangeRate = {
          AUD: 1.0490, // 1AUD = 1.0490 USD
          EUR: 1.4407, // 1EUR = 1.4407 USD
          GBP: 1.6424,
          USD: 1.0
        };
    
    selector.onchange = function () {
        var 
            toCurrency = selector.value.toUpperCase();
    
        for (var i=0,l=currencyElements.length; i" + toCurrency.toUpperCase() + "";
              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

提交回复
热议问题