# Looping thru buttons

**URL:** <https://community.thunkable.com/t/looping-thru-buttons/347640>\
**Category:** Questions about Thunkable X\
**Created:** [December 2, 2019, 6:30pm UTC](https://community.thunkable.com/t/looping-thru-buttons/347640 "2019-12-02T18:30:25Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![townythunk](https://avatars.discourse-cdn.com/v4/letter/t/bb73d2/32.png) [@townythunk](https://community.thunkable.com/u/townythunk)\
**Post date:** [December 2, 2019, 6:30pm UTC](https://community.thunkable.com/t/looping-thru-buttons/347640/1 "2019-12-02T18:30:25Z")

</div>

Hi - can someone please guide me to correct method?  
I have many buttons (say btn\_01, btn\_02 thru btn\_20).  
At some point in my blocking I have a variable that stores a number say varBtnNumber.  
I’d like to set - say the background of btn\_12 to RED when varBtnNumber is 12  
I’d like to set - say the background of btn\_13 to RED when varBtnNumber is 13. etc  
Is there a method whereby I can loop thru the objects (btn’s) and stop when I get last 2 characters of any btnName = varBtnNumber. ?  
ps: I know how to do this in VBA.(Right(btn\_name, 2) = varBtnNumber. )  
Many thx…

---

<div class="post-metadata">

**Author:** ![actech](https://avatars.discourse-cdn.com/v4/letter/a/3d9bf3/32.png) [@actech](https://community.thunkable.com/u/actech)\
**Post date:** [December 2, 2019, 6:42pm UTC](https://community.thunkable.com/t/looping-thru-buttons/347640/2 "2019-12-02T18:42:08Z")

</div>

Hi,

You need to carefully study the components located on the “Any components” tab. Then you will see that with their help you can easily solve your problem - ["Any Component" Blocks - ✕ Docs](https://docs.thunkable.com/any-component-blocks)

 ![изображение](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/9/2/926238624243af27c44cd553c749422e28e3a480.png)

Best regards, Alex  
[ACtech demo project](https://x.thunkable.com/projects/5d70f93f8a62b4079eedaff2/project/properties/designer/)  
[Thunkable X Basic Programming Course](http://droidscript.ru/main/title_ds15.php)  
[Block Reference](http://droidscript.ru/main/statyi/thunkableios_blockhelper.php)  
[Component Reference](http://droidscript.ru/main/statyi/thunkableios_objects.php)  
[Bug tracker](https://x.thunkable.com/projects/5dbd5243a77acad1b26f851f/project/properties/designer/)

---

<div class="post-metadata">

**Author:** ![townythunk](https://avatars.discourse-cdn.com/v4/letter/t/bb73d2/32.png) [@townythunk](https://community.thunkable.com/u/townythunk)\
**Post date:** [December 2, 2019, 7:09pm UTC](https://community.thunkable.com/t/looping-thru-buttons/347640/3 "2019-12-02T19:09:29Z")

</div>

thx Alex - I did see that one - but I still can’t see how I can harness that buttonName (ps: not button text).  
Also the varBtnNumber is not generated from clicking any button.

---

<div class="post-metadata">

**Author:** ![townythunk](https://avatars.discourse-cdn.com/v4/letter/t/bb73d2/32.png) [@townythunk](https://community.thunkable.com/u/townythunk)\
**Post date:** [December 2, 2019, 8:58pm UTC](https://community.thunkable.com/t/looping-thru-buttons/347640/4 "2019-12-02T20:58:35Z")

</div>

Hey Alex - do you think there may be a better way than this below?:  
Make a new list of all buttons (1 thru 20) where 1 represents btn\_01, 2 represents btn\_02 etc.  
then use  
For each item in J in list  
If j = varBtnNumber and j =1 then do the background thing (using btn\_01) else nothing  
If j = varBtnNumber and j =2 then do the background thing (using btn\_02) else nothing  
If j = varBtnNumber and j =3 then do the background thing (using btn\_03) else nothing  
etcetc. thru to all 20 (bit inefficient but it would trap the one button matching the varBtnNumber )

---

<div class="post-metadata">

**Author:** ![actech](https://avatars.discourse-cdn.com/v4/letter/a/3d9bf3/32.png) [@actech](https://community.thunkable.com/u/actech)\
**Post date:** [December 3, 2019, 7:56am UTC](https://community.thunkable.com/t/looping-thru-buttons/347640/5 "2019-12-03T07:56:22Z")

</div>

Open the project by the link, click the Recursion button, and then click any buttons with Button 1 text. Have you been looking for this?

[https://x.thunkable.com/projects/5db93ebab8e0fc9c685b3477/project/properties/designer/](https://x.thunkable.com/projects/5db93ebab8e0fc9c685b3477/project/properties/designer/)  
Best regards, Alex  
[ACtech demo project](https://x.thunkable.com/projects/5d70f93f8a62b4079eedaff2/project/properties/designer/)  
[Thunkable X Basic Programming Course](http://droidscript.ru/main/title_ds15.php)  
[Block Reference](http://droidscript.ru/main/statyi/thunkableios_blockhelper.php)  
[Component Reference](http://droidscript.ru/main/statyi/thunkableios_objects.php)  
[Bug tracker](https://x.thunkable.com/projects/5dbd5243a77acad1b26f851f/project/properties/designer/)

---

<div class="post-metadata">

**Author:** ![townythunk](https://avatars.discourse-cdn.com/v4/letter/t/bb73d2/32.png) [@townythunk](https://community.thunkable.com/u/townythunk)\
**Post date:** [December 3, 2019, 6:40pm UTC](https://community.thunkable.com/t/looping-thru-buttons/347640/6 "2019-12-03T18:40:58Z")

</div>

yr demo helped me thx Alex.  
the way I was trying to do stuff was a little lack of knowledge.  
So yr content certainly does help us newbies get onto a better line.  
very much appreciated.

---

<div class="post-metadata">

**Author:** ![thestrengthinstitute](https://avatars.discourse-cdn.com/v4/letter/t/48db29/32.png) [@thestrengthinstitute](https://community.thunkable.com/u/thestrengthinstitute)\
**Post date:** [May 6, 2020, 9:43am UTC](https://community.thunkable.com/t/looping-thru-buttons/347640/7 "2020-05-06T09:43:29Z")

</div>

Hi @townythunk,

Can you share your solution? I am also trying to do the same but wasn’t able to understand. Absolute newbie here.  
So I have buttons 1-36. I have a variable being set in another screen.  
In the current screen, if button number \< variable, set background color of button to green else white.  
For ex, say variable has been set to 5 in the previous screen. When current screen opens, buttons 1-4 will be green, button 5 will be blue and 6-36 will be white.  
Please help.

Susovan ![Untitled](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/9/2/92e1c46e6045b331e6a70f13274fe62cf95185ad.png)  
Edit: So I have managed to do this. But not sure how I can change the button name to show i-th button. I mean when the loop is 1, i want to change color of button 1, when loop is at 2, I want to change color of button 2.  
Is this possible???

---

<div class="post-metadata">

**Author:** ![thestrengthinstitute](https://avatars.discourse-cdn.com/v4/letter/t/48db29/32.png) [@thestrengthinstitute](https://community.thunkable.com/u/thestrengthinstitute)\
**Post date:** [May 7, 2020, 8:10am UTC](https://community.thunkable.com/t/looping-thru-buttons/347640/8 "2020-05-07T08:10:53Z")

</div>

Ok so I found the solution. Its working as per my requirement now.

![Untitled](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/e/1/e10ab5d23137a27079993e2c53480f611a1c4fc3.png)

---

<div class="post-metadata">

**Author:** ![jared](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/jared/32/138473_2.png) [@jared](https://community.thunkable.com/u/jared)\
**Post date:** [May 7, 2020, 11:21am UTC](https://community.thunkable.com/t/looping-thru-buttons/347640/9 "2020-05-07T11:21:57Z")

</div>

Great work! @thestrengthinstitute

---

<div class="post-metadata">

**Author:** ![ioannis](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/ioannis/32/146956_2.png) [@ioannis](https://community.thunkable.com/u/ioannis)\
**Post date:** [November 8, 2024, 11:29am UTC](https://community.thunkable.com/t/looping-thru-buttons/347640/10 "2024-11-08T11:29:21Z")

</div>


