Ad

How Can I Do An If Statement Based On Option Value? [Liquid / Shopify]

- 1 answer

How can I run an if statement using liquid in Shopify based on the value of an option in a dropdown?

This is my code for the dropdown that I want to extract the information from:

<div class="grid__item large--one-half"> 
  <label for="ContactFormAddressSame" class="">{{ 'contact.form.addressSame' | t }}</label>
  <select id="ContactFormAddressSame" name="contact[addressSame]">
    <option>Yes</option>
    <option>No</option>
  </select>
</div>

Basically what I want to happen is if the value is equal to 'No' then do something. If 'Yes', don't do anything.

Ad

Answer

You need to learn the difference between Server side and Client side.

Liquid is server side template language, what you are asking is to execute some Liquid code once a client has updated his select and you can't do this.

Once you see the select in your browser the server side logic was completed and it won't fire again no matter what you do on the client side.

So your best approach is to create two tabs that will handle the two options of the select and based on the selection show the correct tab using JS.

Ad
source: stackoverflow.com
Ad