Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.

Which Button Was Pressed?

To find out which button was pressed in a component containing multiple buttons, use Component.getLocalId().

Let’s look at an example with multiple lightning:button components. Each button has a unique local ID, set by an aura:id attribute.

1<!--c:buttonPressed-->
2<aura:component>
3    <aura:attribute name="whichButton" type="String" />
4    
5    <p>You clicked: {!v.whichButton}</p>
6
7    <lightning:button aura:id="button1" label="Click me" onclick="{!c.nameThatButton}"/>
8    <lightning:button aura:id="button2" label="Click me too" onclick="{!c.nameThatButton}"/>
9</aura:component>

Use event.getSource() in the client-side controller to get the button component that was clicked. Call getLocalId() to get the aura:id of the clicked button.

1/* buttonPressedController.js */
2({
3    nameThatButton : function(cmp, event, helper) {
4        var whichOne = event.getSource().getLocalId();
5        console.log(whichOne);
6        cmp.set("v.whichButton", whichOne);
7    }
8})
In the client-side controller, you can use one of the following methods to find out which button was clicked.
  • event.getSource().getLocalId() returns the aura:id of the clicked button.
  • event.getSource().get("v.name") returns the name of the clicked button.