Ad

Shopify Liquid Template: On-click Shows Same Address On Different Address Icon's

My End goal is,When I click on first Address Icon It must show the address map bind to it as Iframe same with second one.I have successfully added dynamic class to the Address Icons as addressthen I also successful with on Address Icon click Google map is getting loaded. Main problem is I am getting either of map location i.e Wink & Nod on both time or Abhiruchi Mall. I have checked this by changing value of i value in for loop by changing it to 0 or 1 and changing less than condition, also i shuffled switch case 0 or 1. I am 99% successful with it but on click on both Icons loading same Maps. When I click on first Address Icon Wink & Nod map must be shown and when I click on second Icon Abhiruchi Mall must be loaded.Currently they are loading same address on click of both Icons.Thanks.

Shopify liquid template

<div class="container main content main-wrapper">
  {% if section.settings.image == nil %}
    <h1 class="center">{{ page.title }}</h1>
    <div class="feature_divider"></div>
  {% endif %}

  <div class="sixteen columns featured_links">
    <div class="section clearfix feature">
      {% for block in section.blocks %}
        <div class="{% if section.settings.featured_promos_per_row == 2 %}eight columns {% cycle 'alpha', 'omega' %}{% elsif section.settings.featured_promos_per_row == 3 %}one-third column {% cycle 'alpha', '', 'omega' %}{% else %}four columns {% cycle 'alpha', '', '', 'omega' %}{% endif %} {% if section.settings.featured_links_style != blank %}{{ section.settings.featured_links_style }} {% cycle 'delay-025s', 'delay-05s', 'delay-075s', 'delay-1s' %}{% endif %} center">
          {% if block.settings.link != blank %}
            <a target="_blank" rel="nofollow noreferrer" href="{{ block.settings.link }}">
          {% endif %}

          <div class="{% if section.settings.rounded_image != blank %}rounded{% endif %}">
            {% if block.settings.image != nil %}
              <img  src="{{ block.settings.image | img_url: '300x' }}"
                    alt="{{ block.settings.image.alt }}"
                    data-src="{{ block.settings.image | img_url: '2048x' }}"
                    class="lazyload"
                    {% comment %} data-sizes="auto" {% endcomment %}
                    data-srcset=" {{ block.settings.image | img_url: '2048x' }} 2048w,
                                  {{ block.settings.image | img_url: '1600x' }} 1600w,
                                  {{ block.settings.image | img_url: '1200x' }} 1200w,
                                  {{ block.settings.image | img_url: '1000x' }} 1000w,
                                  {{ block.settings.image | img_url: '800x' }} 800w,
                                  {{ block.settings.image | img_url: '600x' }} 600w,
                                  {{ block.settings.image | img_url: '400x' }} 400w"
                     />
            {% else %}
              {% capture i %}{% cycle "1", "2", "3", "4", "5", "6" %}{% endcapture %}
              {{ 'collection-' | append: i | placeholder_svg_tag: 'placeholder-svg placeholder-svg--promotions' }}
            {% endif %}
          </div>

          {% if block.settings.title != blank %}
            <h3>{{ block.settings.title | escape }}</h3>
            {% if section.settings.show_divider %}
              <div class="feature_divider"></div>
            {% endif %}
          {% endif %}
          {% if block.settings.link != blank %}
            </a>
          {% endif %}

            {% if forloop.index <= 2  %}
                <div class="address"></div>
            {% endif %} 

          {% if block.settings.text != blank %}
            {{ block.settings.text }}
          {% endif %}
        </div>

        {% if section.settings.featured_promos_per_row == 2 %}
          {% cycle '', '<br class="clear " />' %}
        {% elsif section.settings.featured_promos_per_row == 3 %}
          {% cycle '', '', '<br class="clear" />' %}
        {% else %}
          {% cycle '', '', '', '<br class="clear" />' %}
        {% endif %}
      {% endfor %}
    </div>

    {% if section.settings.contact_address != blank %}
    <br class="clear" />
    <div class="embed-container maps">
        <iframe width="100%" height="400" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.com/maps?f=q&amp;source=embed&amp;hl=en&amp;geocode=&amp;q={{ section.settings.contact_address | replace: ' ', '+' }}&amp;z={{ section.settings.zoom_level }}&amp;output=embed"></iframe>
    </div>
    {% endif %}    
  </div>
</div>

JQuery Code onClick load google Map

<style>    
.address{
    position: absolute;
    left: 234px;
    bottom: 160px;
    width: 110px;
    height: 115px;
}
</style>

<script>   
$('.address').click(function(){
for(var i=0;i<=$('.address').length;i++){
            switch (i) {            
                case 0 :
                 $('.embed-container, .maps').html('<iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d945.7084267048235!2d73.8854388291583!3d18.536415399212462!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x0%3A0xdda3387f821de029!2sWink+%26+Nod!5e0!3m2!1sen!2sin!4v1525613363322" width="100%" height="400" frameborder="0" scrolling="no" marginheight="0" marginwidth="0"></iframe>'); 
                break;
                case 1 : 
                $('.embed-container, .maps').html('<iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d3784.450403270009!2d73.81459871445705!3d18.463246687442403!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x3bc2956a20fbac17%3A0x91987f7033fd2e3c!2sAbhiruchi+Mall+and+Multiplex!5e0!3m2!1sen!2sin!4v1525612885113" width="100%" height="400" frameborder="0" scrolling="no" marginheight="0" marginwidth="0"></iframe>'); 
                break;    
                default:
                $('.embed-container, .maps').html(' <iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d945.7084267048235!2d73.8854388291583!3d18.536415399212462!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x0%3A0xdda3387f821de029!2sWink+%26+Nod!5e0!3m2!1sen!2sin!4v1525613363322" width="100%" height="400" frameborder="0" scrolling="no" marginheight="0" marginwidth="0"></iframe>'); 
                break;     
            }
        }
  });
</script>
Ad

Answer

I have refactored my script to this and it worked like charm !!!

$('.address').click(function(){
   var j=$('.address').index(this);    
  switch (j) {            
                case 0 :
                 $('.embed-container, .maps').html('<iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d945.7084267048235!2d73.8854388291583!3d18.536415399212462!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x0%3A0xdda3387f821de029!2sWink+%26+Nod!5e0!3m2!1sen!2sin!4v1525613363322" width="100%" height="400" frameborder="0" scrolling="no" marginheight="0" marginwidth="0"></iframe>'); 
                break;
                case 1 : 
                $('.embed-container, .maps').html('<iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d3784.450403270009!2d73.81459871445705!3d18.463246687442403!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x3bc2956a20fbac17%3A0x91987f7033fd2e3c!2sAbhiruchi+Mall+and+Multiplex!5e0!3m2!1sen!2sin!4v1525612885113" width="100%" height="400" frameborder="0" scrolling="no" marginheight="0" marginwidth="0"></iframe>'); 
                break;    
                default:
                $('.embed-container, .maps').html(' <iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d945.7084267048235!2d73.8854388291583!3d18.536415399212462!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x0%3A0xdda3387f821de029!2sWink+%26+Nod!5e0!3m2!1sen!2sin!4v1525613363322" width="100%" height="400" frameborder="0" scrolling="no" marginheight="0" marginwidth="0"></iframe>'); 
                break;     
            }

  });

</script>
Ad
source: stackoverflow.com
Ad