Is it possible to recalculate currency with Javascript or Jquery.
For example:
<div id="price">$99.00</div>
would be changed to
<div class="gbp" id="price">£63.85</div>
If a class of "GBP" was applied to the div tag?
Is it possible to recalculate currency with Javascript or Jquery.
For example:
<div id="price">$99.00</div>
would be changed to
<div class="gbp" id="price">£63.85</div>
If a class of "GBP" was applied to the div tag?
 
    
    You can do that using MutationObservers:
(function() {
  var observer = new MutationObserver(function(mutations) {
    // for the class attribute, only the last mutation is important
    var mutation = mutations[mutations.length - 1],
        el = mutation.target;
    calculatePrice(el);
  });
  var config = {
    attributes: true,
    attributeFilter: ['class']
  };
  document.addEventListener('DOMContentLoaded', function() {
    var price = document.querySelector('#price');
    observer.observe(price, config);
    [].forEach.call(document.querySelectorAll('button'), function(btn, i) {
      btn.addEventListener('click', function(e) {
        price.className = this.id;
      });
    });
  });
  function calculatePrice(el) {
    var currencies = {
      usd: 1,
      gbp: 0.65,
      brl: 3.90
    };
    
    var types = {
      usd: '$',
      gbp: '£',
      brl: 'R$'
    };
    var currentType = Object.keys(types).filter(function(t) {
      return el.textContent.slice(0, types[t].length) === types[t];
    });
    
    currentType = currentType[0];
    
    var newType = el.className;
    
    var vl = Number(el.getAttribute('data-price')) * currencies[newType];
    el.textContent = types[newType] + vl.toFixed(2);
  }
})();<div id='price' data-price='99' class='usd'>$99.00</div>
<button id='usd'>Change the class to 'usd'</button>
<button id='gbp'>Change the class to 'gbp'</button>
<button id='brl'>Change the class to 'brl'</button>As you can see, the button's event handlers only changes the className of the input:
btn.addEventListener('click', function(e) {
  price.className = this.id;
});
And the observer gets that change, and do the conversion for you.
