1

We inherited some old code which is in place and working. It is a simple pricing switch between £ GBP and $ USD - the price updates onChange as seen below in the pen.

PEN link

https://codepen.io/go6/pen/rNOgaee

$(function() {
  $("#curr").on("change",function() {
    var curr   = this.value;
    var prefix = curr=="500"; // or ["usd","yen",...].indexOf(curr); for more
    var sign   = curr=="500"?"":"";
    $(".value").each(function(){
      $(this).text(
        (prefix?sign:"")   +
                $(this).data(curr) +
    (prefix?sign:"")
      );
    })
  }).change();
});


Question

The price by default is set to USD, as per the data-usd value when the page loads. We want to supply a page URL query string eg /pricing?curr=GBP which can toggle the pricing to GBP.


Tried so far but not working

I have tried a few combinations using this Answer:

Show / Hide elements based on query string value

but so far, none have worked. Please can someone assist in implementing a query string sitch of currency?


Wayne
  • 13
  • 3

1 Answers1

0

Just add this to your code:

 if(document.URL.indexOf("curr=GBP") !== -1)
 {
    $(".value").each(function(){
       $(this).text($(this).data("gbp"));
    })
    $("#curr").val("gbp");
 }
matthias_h
  • 11,356
  • 9
  • 22
  • 40
  • Nearly there. Thank you. The price updates but the select dropdown val doesn't. – Wayne May 26 '20 at 18:16
  • @wayne That's strange as it should be !== -1 to check if the string is there. For the select dropdown issue I have just updated my answer. – matthias_h May 26 '20 at 18:22
  • @Wayne Glad that I was able to help you. If this answered your question, please check the checkmark next to the answer to resolve your question as answered/solved. – matthias_h May 27 '20 at 14:38