# How can I get my jQuery function to loop?

**URL:** <https://forum.freecodecamp.org/t/how-can-i-get-my-jquery-function-to-loop/80910>\
**Category:** General\
**Created:** [January 26, 2017, 8:05pm UTC](https://forum.freecodecamp.org/t/how-can-i-get-my-jquery-function-to-loop/80910 "2017-01-26T20:05:18Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![zacattack17](https://avatars.discourse-cdn.com/v4/letter/z/a183cd/32.png) [@zacattack17](https://forum.freecodecamp.org/u/zacattack17)\
**Post date:** [January 26, 2017, 8:05pm UTC](https://forum.freecodecamp.org/t/how-can-i-get-my-jquery-function-to-loop/80910/1 "2017-01-26T20:05:18Z")

</div>

So I am in beginning stages of making my portfolio on here, and a feature I’ve added is one that when someone clicks a button, the title changes to another language. I’ve successfully gotten that to happen, however, I want to make it loop. Below is my code, can you help me do so?

[http://codepen.io/zb1794/full/VPzWVv/](http://codepen.io/zb1794/full/VPzWVv/)

Thank you very much for any help!

---

<div class="post-metadata">

**Author:** ![Shin8](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/shin8/32/162313_2.png) [@Shin8](https://forum.freecodecamp.org/u/Shin8)\
**Post date:** [January 26, 2017, 8:13pm UTC](https://forum.freecodecamp.org/t/how-can-i-get-my-jquery-function-to-loop/80910/2 "2017-01-26T20:13:40Z")

</div>

Maybe with setInterval(function, number).

---

<div class="post-metadata">

**Author:** ![Shin8](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/shin8/32/162313_2.png) [@Shin8](https://forum.freecodecamp.org/u/Shin8)\
**Post date:** [January 26, 2017, 8:19pm UTC](https://forum.freecodecamp.org/t/how-can-i-get-my-jquery-function-to-loop/80910/4 "2017-01-26T20:19:54Z")

</div>

Opps. Swoodend is right, put them in an array and then you can do the math so it loops (I remember there was a very nice way to do that with the % operator).

My first post had nothing to do with the question xD but after getting that loop function right you can use that interval function so it changes automatically.

---

<div class="post-metadata">

**Author:** ![zacattack17](https://avatars.discourse-cdn.com/v4/letter/z/a183cd/32.png) [@zacattack17](https://forum.freecodecamp.org/u/zacattack17)\
**Post date:** [January 26, 2017, 8:21pm UTC](https://forum.freecodecamp.org/t/how-can-i-get-my-jquery-function-to-loop/80910/5 "2017-01-26T20:21:09Z")

</div>

Would it be possible for you to maybe show me? I don’t understand

---

<div class="post-metadata">

**Author:** ![Swoodend](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/swoodend/32/18707_2.png) [@Swoodend](https://forum.freecodecamp.org/u/Swoodend)\
**Post date:** [January 26, 2017, 8:21pm UTC](https://forum.freecodecamp.org/t/how-can-i-get-my-jquery-function-to-loop/80910/6 "2017-01-26T20:21:40Z")

</div>

I think I misunderstood what OP was asking for so I withdrew my post 😛

---

<div class="post-metadata">

**Author:** ![zacattack17](https://avatars.discourse-cdn.com/v4/letter/z/a183cd/32.png) [@zacattack17](https://forum.freecodecamp.org/u/zacattack17)\
**Post date:** [January 26, 2017, 8:23pm UTC](https://forum.freecodecamp.org/t/how-can-i-get-my-jquery-function-to-loop/80910/7 "2017-01-26T20:23:54Z")

</div>

Basically I want to make the title change to different languages with the button, but the languages stop, I want it to cycle through the languages and never stop.

---

<div class="post-metadata">

**Author:** ![Swoodend](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/swoodend/32/18707_2.png) [@Swoodend](https://forum.freecodecamp.org/u/Swoodend)\
**Post date:** [January 26, 2017, 8:32pm UTC](https://forum.freecodecamp.org/t/how-can-i-get-my-jquery-function-to-loop/80910/8 "2017-01-26T20:32:04Z")

</div>

OK. So every time the user clicks the button they see a different language. To do this you don’t really need to loop at all.

One option is to store all the phrases you want to display (in each language) in an array. When the user clicks a the button a random number is chosen. That random number is used to select a phrase from the array and it is displayed on your page.

If you find yourself stuck with this I would recommend going over the course material again to really solidify your understanding of loops, syntax, etc.

---

<div class="post-metadata">

**Author:** ![Shin8](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/shin8/32/162313_2.png) [@Shin8](https://forum.freecodecamp.org/u/Shin8)\
**Post date:** [January 26, 2017, 8:35pm UTC](https://forum.freecodecamp.org/t/how-can-i-get-my-jquery-function-to-loop/80910/9 "2017-01-26T20:35:06Z")

</div>

Your answer was right, I was the one that read something else xD

@zacattack17  
Instead of doing h3 .text( something ) for each item you can create an array and put everything in there: var array = [‘first’,‘second’,‘third’,‘another one’];

Then you can use for loops and the array’s index to create those h3 elements. Then the math thing so you can reset the index to 0 once it reaches its length and set that up with the click event of the button.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 16, 2021, 5:11pm UTC](https://forum.freecodecamp.org/t/how-can-i-get-my-jquery-function-to-loop/80910/10 "2021-01-16T17:11:54Z")

</div>

Closed on January 16, 2021, 5:11pm UTC


