<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: JavaScript affecting multiple instances of a module on landing page in CMS Development</title>
    <link>https://community.hubspot.com/t5/CMS-Development/JavaScript-affecting-multiple-instances-of-a-module-on-landing/m-p/268835#M12145</link>
    <description>&lt;P&gt;Thanks so much, Stephanie! This looks like exactly what I need.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 07 May 2019 14:30:15 GMT</pubDate>
    <dc:creator>lgriffee</dc:creator>
    <dc:date>2019-05-07T14:30:15Z</dc:date>
    <item>
      <title>JavaScript affecting multiple instances of a module on landing page</title>
      <link>https://community.hubspot.com/t5/CMS-Development/JavaScript-affecting-multiple-instances-of-a-module-on-landing/m-p/268251#M12104</link>
      <description>&lt;P&gt;I'm trying to code a generic horizontal scroll module that ideally would be used several times on a landing page with the ability to add different content to each instance of the module.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The issue I've run into is that when there are multiple instances of this module on one page they all trigger each other. Since the class/variable names of each instance of the module are the same, when you click the button to scroll one of them it scrolls all of them (&lt;A href="http://info.nols.edu/-temporary-slug-a3fa2af1-bcc6-4d6b-a45c-e115ce2692ca?hs_preview=OJvZFHsJ-8973750403" target="_self"&gt;see example here&lt;/A&gt;).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is it possible to somehow add unique class identifiers for each instance of the module in both HTML/HubL and JavaScript? I've looked into using the {{name}} HubL token to leverage the unique module name but can't figure out a way I could possibly reference this in JavaScript.&amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have been able to get the functionality I want by creating two separate modules with different class names and JS variable names (&lt;A href="https://info.nols.edu/gap-year" target="_self"&gt;example on this page&lt;/A&gt;) but was hoping there might be a way to do this for a generic module that you could easily add different content to it?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;HTML:&lt;/P&gt;&lt;PRE&gt;&amp;lt;div class="horizontal-scroll-grid"&amp;gt;
  &amp;lt;button class="horizontal-scroll-grid__left-button"&amp;gt;
      &amp;lt;i class="horizontal-scroll-grid__button__icon fa fa-chevron-left" role="img" aria-label="Previous" focusable="false"&amp;gt;&amp;lt;/i&amp;gt;
  &amp;lt;/button&amp;gt;
  
  &amp;lt;button class="horizontal-scroll-grid__right-button"&amp;gt;
      &amp;lt;i class="horizontal-scroll-grid__button__icon fa fa-chevron-right" role="img" aria-label="Next" focusable="false"&amp;gt;&amp;lt;/i&amp;gt;
  &amp;lt;/button&amp;gt;
  
  &amp;lt;ul class="horizontal-scroll-grid__scroll-grid"&amp;gt;
    {% for item in module.scroll_item %}
      &amp;lt;li class="horizontal-scroll-grid__scroll-grid__item" style="background-image: url('{{ item.image.src }}');"&amp;gt;
        &amp;lt;a class="horizontal-scroll-grid__scroll-grid__item__link" href="{{ item.link_url }}" alt="Learn more about {{ item.title }}" target="_blank"&amp;gt;
            &amp;lt;h4 class="horizontal-scroll-grid__scroll-grid__item__title"&amp;gt;{{ item.title }}&amp;lt;/h4&amp;gt;
          &amp;lt;/a&amp;gt;
      &amp;lt;/li&amp;gt;
    {% endfor %}
  &amp;lt;/ul&amp;gt;
&amp;lt;/div&amp;gt;&lt;/PRE&gt;&lt;P&gt;Javascript&amp;amp;colon;&lt;/P&gt;&lt;PRE&gt;/* ==========================================================================
   HORIZONTAL SCROLL GRID MODULE
   ========================================================================== */

$(document).ready(function(){
 
  /* Page Setup
   ========================================================================== */
  // Make sure left scroll button is hidden by default on desktop
  $('.horizontal-scroll-grid__left-button').hide();
  
/* Events
   ========================================================================== */
  var scrollDistance = parseInt($('.horizontal-scroll-grid__scroll-grid__item').width()) + 
                         parseInt($('.horizontal-scroll-grid__scroll-grid').css('grid-column-gap').replace(/[^-\d\.]/g, ''));
  var maxScrollPosition = ($('.horizontal-scroll-grid__scroll-grid li').length - 2);
  var scrollPosition = 0; 
  
  // Handle content scrolling when left button clicked
  $('.horizontal-scroll-grid__left-button').click(function() { 
    event.preventDefault();
    $('.horizontal-scroll-grid__scroll-grid').animate({ scrollLeft: "-=" + scrollDistance + "px" });
    
    scrollPosition--; 
    
    // Hide the left button when there is no more content to scroll, otherwise show
    if (scrollPosition == 0){
      $('.horizontal-scroll-grid__left-button').hide();
    }else if (scrollPosition &amp;gt; 0 &amp;amp;&amp;amp; scrollPosition &amp;lt; maxScrollPosition){
      $('.horizontal-scroll-grid__left-button').show();
      $('.horizontal-scroll-grid__right-button').show();
    }
   });
  
  // Handle content scrolling when right button clicked
  $('.horizontal-scroll-grid__right-button').click(function() {
    event.preventDefault();
    $('.horizontal-scroll-grid__scroll-grid').animate({ scrollLeft: "+=" + scrollDistance + "px" });
    
    scrollPosition++; 
    
    // Hide the right button when there is no more content to scroll, otherwise show
    if (scrollPosition == maxScrollPosition){
      $('.horizontal-scroll-grid__right-button').hide();
    }else if (scrollPosition &amp;gt; 0 &amp;amp;&amp;amp; scrollPosition &amp;lt; maxScrollPosition){
      $('.horizontal-scroll-grid__left-button').show();
      $('.horizontal-scroll-grid__right-button').show();
    }
  });
  
  // Reset grid when window is resized 
  $(window).resize(function() {
     scrollPosition = 0; 
    scrollDistance = parseInt($('.horizontal-scroll-grid__scroll-grid__item').width()) + 
                       parseInt($('.horizontal-scroll-grid__scroll-grid').css('grid-column-gap').replace(/[^-\d\.]/g, ''));
    
    $('.horizontal-scroll-grid__scroll-grid').scrollLeft(0);
   
    if ($(window).width() &amp;lt; 1024){
      $('.horizontal-scroll-grid__left-button').hide();
      $('.horizontal-scroll-grid__right-button').hide();
    }else{
      $('.horizontal-scroll-grid__left-button').hide();
      $('.horizontal-scroll-grid__right-button').show();
    }
  });
}); &lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 May 2019 22:35:12 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/JavaScript-affecting-multiple-instances-of-a-module-on-landing/m-p/268251#M12104</guid>
      <dc:creator>lgriffee</dc:creator>
      <dc:date>2019-05-02T22:35:12Z</dc:date>
    </item>
    <item>
      <title>Re: JavaScript affecting multiple instances of a module on landing page</title>
      <link>https://community.hubspot.com/t5/CMS-Development/JavaScript-affecting-multiple-instances-of-a-module-on-landing/m-p/268407#M12113</link>
      <description>&lt;P&gt;Hey!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I often do in these cases is use {{ mid }} to generate a unique ID for that instance of that module (check out &lt;A href="https://integrate.hubspot.com/t/how-to-access-to-the-id-of-the-current-custom-module/10407/4#post_4" target="_self"&gt;this workaround&lt;/A&gt;) and then use that in my JavaScript.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could either pull it in your script by adding the ID to a data attribute or you can add some of your script to &amp;lt;script&amp;gt; tags in the HTML using &lt;A href="https://designers.hubspot.com/en/docs/hubl/hubl-supported-functions#require-js" target="_self"&gt;require_js&lt;/A&gt; so that you can access the HubL there.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I hope that helps!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;
&lt;P&gt;&lt;FONT size="5"&gt;&lt;STRONG&gt;Stephanie O'Gay Garcia&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;HubSpot Design / Development&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.stephanieogaygarcia.com?utm_source=HubSpotCommunity" target="_blank"&gt;Website&lt;/A&gt; | &lt;A href="http://www.stephanieogaygarcia.com/contact?utm_source=HubSpotCommunity" target="_blank"&gt;Contact&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#32C43F"&gt;&lt;I&gt;If this helped, please mark it as the solution to your question, thanks!&lt;/I&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 03 May 2019 16:44:25 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/JavaScript-affecting-multiple-instances-of-a-module-on-landing/m-p/268407#M12113</guid>
      <dc:creator>Stephanie-OG</dc:creator>
      <dc:date>2019-05-03T16:44:25Z</dc:date>
    </item>
    <item>
      <title>Re: JavaScript affecting multiple instances of a module on landing page</title>
      <link>https://community.hubspot.com/t5/CMS-Development/JavaScript-affecting-multiple-instances-of-a-module-on-landing/m-p/268835#M12145</link>
      <description>&lt;P&gt;Thanks so much, Stephanie! This looks like exactly what I need.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 May 2019 14:30:15 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/JavaScript-affecting-multiple-instances-of-a-module-on-landing/m-p/268835#M12145</guid>
      <dc:creator>lgriffee</dc:creator>
      <dc:date>2019-05-07T14:30:15Z</dc:date>
    </item>
    <item>
      <title>Re: JavaScript affecting multiple instances of a module on landing page</title>
      <link>https://community.hubspot.com/t5/CMS-Development/JavaScript-affecting-multiple-instances-of-a-module-on-landing/m-p/470788#M24312</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've been doing this for a year or two. If I understand correctly the point here, you can do that:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;&amp;lt;div id="my_prefix_id_{{ name }}"&amp;gt;Stuff&amp;lt;/div&amp;gt;

&amp;lt;style&amp;gt;
#my_prefix_id_{{ name }} {
   color: purple;
}
&amp;lt;/style&amp;gt;

&amp;lt;script&amp;gt;
document.getElementById('my_prefix_id_{{ name }}').style.color = "purple";
&amp;lt;/script&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here, the code is visible in the source of your page. But if you want the code to appear in its respective files, do this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;&amp;lt;div id="my_prefix_id_{{ name }}"&amp;gt;Stuff&amp;lt;/div&amp;gt;

{% require_css %}
&amp;lt;style&amp;gt;
#my_prefix_id_{{ name }} {
   color: purple;
}
&amp;lt;/style&amp;gt;
{% end_require_css %}

{% require_js %}
&amp;lt;script&amp;gt;
// You can use this value in a global js script.
let my_id = 'my_prefix_id_{{ name }}';
&amp;lt;/script&amp;gt;
{% end_require_js %}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;By doing this, you have a unique id for each instance of your module on the same page (add several times the module on a page, and you'll see each id will be unique), but the code of your module is unique too.&lt;/P&gt;</description>
      <pubDate>Thu, 31 Mar 2022 09:19:56 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/JavaScript-affecting-multiple-instances-of-a-module-on-landing/m-p/470788#M24312</guid>
      <dc:creator>sylvain_pref</dc:creator>
      <dc:date>2022-03-31T09:19:56Z</dc:date>
    </item>
  </channel>
</rss>

