I am currently developing a store for my client on Shopify. He wants me to add a functionality by which the user ‘s IP address is tracked and he gets to see the currency in of his own particular country. I am using the following code:
I want to change the selected value in dropdown to “AUD”
I only get some syntax error in this part:
jQuery("#select_selector option:selected").removeAttr("selected");
jQuery("#select_selector option:[value='"AUD"']").attr('selected', 'selected')
It says wrong syntax or arguement missing.
3
Answers
You need to concatenate your strings and variables. You can do this with the
+
operator. You also don’t need the colon afteroption
If
AUD
is not a variable, then you should just remove the quotes altogether.@Get Off My Lawn’s code is working fine.
or try this JSFiddle
And it will be good to have AUD/USD or any currency as a variable.
option:[value='"AUD"']
should beoption[value=AUD]
. No colon needed.But actually you can just use
jQuery("#select_selector").val('AUD');
if you were using the samples from https://help.shopify.com/themes/customization/currencies/show-multiple-currencies (which I recommend)
then that would be
jQuery("#currencies").val('AUD');
Not if you are auto selecting the currency for first time visitors you should also try storing the currency selection in local storage. Not only will that save you a geoip look-up it will also let customers override that value to the currency of their choice. Shopify’s sample uses a cookie referred to as
cookieCurrency
in their code so your GeoIP lookup could first test that and only do the lookup if it is not set.