How to execute a single function on all List items

Hi,

Lets consider a component Button which is having a function changeName(). Now there is a List ‘ButtonsList’ with a few Button components as its items.

Is there any way I can execute the changeName() function of all the buttons which are children of this ButtonsList ?

Hi @Sirisha_p,

Lightning doesn’t have any specific method of executing a function on all children. But the functionality you are after can be easily built yourself.

The most straightforward method is to grab all children (this.childList) of your ButtonList component, iterate over them and execute your changeName function on each Button instance.

Alternatively you could go the Event emitter route, and emit some kind of “name-change” event that each Button is listening to.

Remember that in the end a Lightning component is just a Javascript class, so you have quite some freedom in how you interact with them as you would with any other Javascript object.