# JavaScript affecting multiple instances of a module on landing page

**URL:** <https://community.hubspot.com/t/javascript-affecting-multiple-instances-of-a-module-on-landing-page/14208>\
**Category:** CMS Development\
**Tags:** design-manager, javascript\
**Created:** [May 2, 2019, 10:35pm UTC](https://community.hubspot.com/t/javascript-affecting-multiple-instances-of-a-module-on-landing-page/14208 "2019-05-02T22:35:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lgriffee](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/lgriffee/32/86265_2.png) [@lgriffee](https://community.hubspot.com/u/lgriffee)\
**Post date:** [May 2, 2019, 10:35pm UTC](https://community.hubspot.com/t/javascript-affecting-multiple-instances-of-a-module-on-landing-page/14208/1 "2019-05-02T22:35:12Z")

</div>

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.

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 ([see example here](http://info.nols.edu/-temporary-slug-a3fa2af1-bcc6-4d6b-a45c-e115ce2692ca?hs_preview=OJvZFHsJ-8973750403)).

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.

I have been able to get the functionality I want by creating two separate modules with different class names and JS variable names ([example on this page](https://info.nols.edu/gap-year)) but was hoping there might be a way to do this for a generic module that you could easily add different content to it?

HTML:

```auto
<div class="horizontal-scroll-grid">
 <button class="horizontal-scroll-grid__left-button">
 <i class="horizontal-scroll-grid __button__ icon fa fa-chevron-left" role="img" aria-label="Previous" focusable="false"></i>
 </button>

 <button class="horizontal-scroll-grid__right-button">
 <i class="horizontal-scroll-grid __button__ icon fa fa-chevron-right" role="img" aria-label="Next" focusable="false"></i>
 </button>

 <ul class="horizontal-scroll-grid__scroll-grid">
 {% for item in module.scroll_item %}
 <li class="horizontal-scroll-grid __scroll-grid__ item" style="background-image: url('{{ item.image.src }}');">
 <a class="horizontal-scroll-grid __scroll-grid__ item__link" href="{{ item.link_url }}" alt="Learn more about {{ item.title }}" target="_blank">
 <h4 class="horizontal-scroll-grid __scroll-grid__ item__title">{{ item.title }}</h4>
 </a>
 </li>
 {% endfor %}
 </ul>
</div>

```

Javascript&colon;

```auto
/* ==========================================================================
 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 > 0 && scrollPosition < 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 > 0 && scrollPosition < 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() < 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();
 }
 });
}); 

```

---

<div class="post-metadata">

**Author:** ![Stephanie-OG](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/stephanie-og/32/73267_2.png) [@Stephanie-OG](https://community.hubspot.com/u/Stephanie-OG)\
**Post date:** [May 3, 2019, 4:44pm UTC](https://community.hubspot.com/t/javascript-affecting-multiple-instances-of-a-module-on-landing-page/14208/2 "2019-05-03T16:44:25Z")

</div>

Hey!

What I often do in these cases is use {{ mid }} to generate a unique ID for that instance of that module (check out [this workaround](https://integrate.hubspot.com/t/how-to-access-to-the-id-of-the-current-custom-module/10407/4#post_4)) and then use that in my JavaScript.

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 \<script\> tags in the HTML using [require\_js](https://designers.hubspot.com/en/docs/hubl/hubl-supported-functions#require-js) so that you can access the HubL there.

I hope that helps!

* * *

**Stephanie O’Gay Garcia**

HubSpot Design / Development

[Website](http://www.stephanieogaygarcia.com?utm_source=HubSpotCommunity) | [Contact](http://www.stephanieogaygarcia.com/contact?utm_source=HubSpotCommunity)

_If this helped, please mark it as the solution to your question, thanks!_

---

<div class="post-metadata">

**Author:** ![lgriffee](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/lgriffee/32/86265_2.png) [@lgriffee](https://community.hubspot.com/u/lgriffee)\
**Post date:** [May 7, 2019, 2:30pm UTC](https://community.hubspot.com/t/javascript-affecting-multiple-instances-of-a-module-on-landing-page/14208/3 "2019-05-07T14:30:15Z")

</div>

Thanks so much, Stephanie! This looks like exactly what I need.

---

<div class="post-metadata">

**Author:** ![sylvain\_pref](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/sylvain_pref/32/77260_2.png) [@sylvain\_pref](https://community.hubspot.com/u/sylvain_pref)\
**Post date:** [August 2, 2021, 1:43pm UTC](https://community.hubspot.com/t/javascript-affecting-multiple-instances-of-a-module-on-landing-page/14208/4 "2021-08-02T13:43:04Z")

</div>

Hi,

I’ve been doing this for a year or two. If I understand correctly the point here, you can do that:

```lia-code-sample
<div id="my_prefix_id_{{ name }}">Stuff</div>

<style>
#my_prefix_id_{{ name }} {
 color: purple;
}
</style>

<script>
document.getElementById('my_prefix_id_{{ name }}').style.color = "purple";
</script>

```

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:

```lia-code-sample
<div id="my_prefix_id_{{ name }}">Stuff</div>

{% require_css %}
<style>
#my_prefix_id_{{ name }} {
 color: purple;
}
</style>
{% end_require_css %}

{% require_js %}
<script>
// You can use this value in a global js script.
let my_id = 'my_prefix_id_{{ name }}';
</script>
{% end_require_js %}

```

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.
