# Using require\_js in a loop in a module

**URL:** <https://community.hubspot.com/t/using-require-js-in-a-loop-in-a-module/52652>\
**Category:** CMS Development\
**Created:** [March 7, 2022, 3:53pm UTC](https://community.hubspot.com/t/using-require-js-in-a-loop-in-a-module/52652 "2022-03-07T15:53:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ADuff](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/aduff/32/97852_2.png) [@ADuff](https://community.hubspot.com/u/ADuff)\
**Post date:** [March 7, 2022, 3:53pm UTC](https://community.hubspot.com/t/using-require-js-in-a-loop-in-a-module/52652/1 "2022-03-07T15:53:04Z")

</div>

Hi,

I’m sure I’ve sat in front of a monitor for too long and my brain has simply shut down…

I’ve built a module which encorporates a JQuery script to toggle “More Info”.

`{% require_js %}<script>$(".room-container-{{loop.index}} .more-details").click(function(){FUNCTION});</script>{% end_require_js %}`

The issue is the script is only appearing in the footer for loop 1. So only the first read more function works. In the footer all I see is:

`<script>$(".room-container-1 .more-details").click(function(){FUNCTION});</script>`

No mention of .room-container-2, 3 etc. What’s the solution?

---

<div class="post-metadata">

**Author:** ![dennisedson](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dennisedson/32/28044_2.png) [@dennisedson](https://community.hubspot.com/u/dennisedson)\
**Post date:** [March 8, 2022, 7:59pm UTC](https://community.hubspot.com/t/using-require-js-in-a-loop-in-a-module/52652/2 "2022-03-08T19:59:40Z")

</div>

@ADuff , is the function within the loop?

@Oezcan , how would you solve for this?

---

<div class="post-metadata">

**Author:** ![ADuff](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/aduff/32/97852_2.png) [@ADuff](https://community.hubspot.com/u/ADuff)\
**Post date:** [March 9, 2022, 8:53am UTC](https://community.hubspot.com/t/using-require-js-in-a-loop-in-a-module/52652/3 "2022-03-09T08:53:13Z")

</div>

Hi, yes it is in a loop.

---

<div class="post-metadata">

**Author:** ![Oezcan](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/oezcan/32/53959_2.png) [@Oezcan](https://community.hubspot.com/u/Oezcan)\
**Post date:** [March 9, 2022, 2:17pm UTC](https://community.hubspot.com/t/using-require-js-in-a-loop-in-a-module/52652/4 "2022-03-09T14:17:05Z")

</div>

Hello @ADuff and @dennisedson

I think you will need something like this.

```auto
var i;
for (i = 0; i < substr.length; ++i) {
 // do something with `substr[i]`
}

```

or in ES2015+:

```auto
for (let i = 0; i < substr.length; ++i) {
 // do something with `substr[i]`
}

```

Here are some other example:

[https://www.sitepoint.com/jquery-each-function-examples/#:~:text=jQuery’s%20each()%20function%20is,arbitrary%20arrays%20and%20object%20properties](https://www.sitepoint.com/jquery-each-function-examples/#:~:text=jQuery's%20each()%20function%20is,arbitrary%20arrays%20and%20object%20properties).

I hope I could help.

Best regards,

Özcan

---

<div class="post-metadata">

**Author:** ![Indra](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/indra/32/98381_2.png) [@Indra](https://community.hubspot.com/u/Indra)\
**Post date:** [March 11, 2022, 9:34am UTC](https://community.hubspot.com/t/using-require-js-in-a-loop-in-a-module/52652/5 "2022-03-11T09:34:41Z")

</div>

Hi @ADuff,

As @Oezcan is telling, it’s best to do a loop within your code without repeating your script multiple times.

This will improve your sitespeed and make your script more readable.

So your html sample will be:

```lia-code-sample
<ul class="list">
	<li class="item item-1"></li>
	<li class="item item-2"></li>
	<li class="item item-3"></li>
</ul>

```

So for example doing:

```lia-code-sample
{% for row in rows %}
{% require_js %}
<script>
var item = document.querySelector('.list > .item-{{ loop.index }}');

item.addEventListener('click', function (event) {
	// Do something...
});
</script>
{% end_require_js %}
{% endfor %}

```

You better do this inside the js tab:

```lia-code-sample
var items = document.querySelectorAll('.list > .item');

items.forEach(function (item, index) {
	item.addEventListener('click', function (event) {
		// Do something...
	});
});

```

Do you have a demo link for your code? This will help solving your problem.

---

<div class="post-metadata">

**Author:** ![ADuff](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/aduff/32/97852_2.png) [@ADuff](https://community.hubspot.com/u/ADuff)\
**Post date:** [March 15, 2022, 2:05pm UTC](https://community.hubspot.com/t/using-require-js-in-a-loop-in-a-module/52652/6 "2022-03-15T14:05:14Z")

</div>

Hi,

I was able to use JQuery to achieve this, but essentially using your idea:

```lia-code-sample
$('.room-container').each(function(){
$(this).on('click', '.more-details', function(){
// functions
});
});

```

---

<div class="post-metadata">

**Author:** ![Indra](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/indra/32/98381_2.png) [@Indra](https://community.hubspot.com/u/Indra)\
**Post date:** [March 16, 2022, 8:36am UTC](https://community.hubspot.com/t/using-require-js-in-a-loop-in-a-module/52652/7 "2022-03-16T08:36:57Z")

</div>

@ADuff Great to here you have fixed your problem. If your problem is solved. Please mark your problem as solved 😊
