skip to Main Content

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


  1. You need to concatenate your strings and variables. You can do this with the + operator. You also don’t need the colon after option

    jQuery("#select_selector option[selected]").removeAttr("selected");
    
    jQuery("#select_selector option[value='" + AUD + "']").attr('selected', 'selected')
    

    If AUD is not a variable, then you should just remove the quotes altogether.

    jQuery("#select_selector option[value=AUD]").attr('selected', 'selected')
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <select id="select_selector">
          <option value="ABC">ABC</option>
          <option value="123">123</option>
          <option value="AUD">AUD</option>
          <option value="EFG">EFG</option>
    </select>
    Login or Signup to reply.
  2. @Get Off My Lawn’s code is working fine.

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <select id="select_selector">
          <option value="ABC">ABC</option>
          <option value="123">123</option>
          <option value="AUD">AUD</option>
          <option value="EFG">EFG</option>
    </select>
    
    <input type="button" value="click me" id="clickme">
    
    
    <script>
     jQuery("#clickme").click(function(){
        jQuery("#select_selector option[selected]").removeAttr("selected");
        jQuery("#select_selector option[value=AUD]").attr('selected', 'selected');
    
    });
    </script>
    

    or try this JSFiddle

    And it will be good to have AUD/USD or any currency as a variable.

    Login or Signup to reply.
  3. option:[value='"AUD"'] should be option[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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search