Newer Version Available

This content describes an older version of this product. View Latest

Which Button Was Pressed?

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

Let’s look at a component that contains multiple buttons. 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    <ui:button aura:id="button1" label="Click me" press="{!c.nameThatButton}"/>
8    <ui:button aura:id="button2" label="Click me too" press="{!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})