Tuesday, February 7, 2017

How to know which button was pressed in lightning component?

How to know which button was pressed in lightning component?


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.

<!--c:buttonPressed-->
<aura:component >
    <aura:attribute name="whichButton" type="String" />
    
    <p>You clicked: {!v.whichButton}</p>

    <ui:button aura:id="button1" label="Click me" press="{!c.nameThatButton}"/>
    <ui:button aura:id="button2" label="Click me too" press="{!c.nameThatButton}"/>
</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.

/* buttonPressedController.js */
({
    nameThatButton : function(cmp, event, helper) {
        var whichOne = event.getSource().getLocalId();
        console.log(whichOne);
        cmp.set("v.whichButton", whichOne);
    }
})


Example Component: 



Controller.Js  : Highlighted code



out put:





for reference : 

https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/js_cb_which_button_pressed.htm


11 comments:

Thamizharasi J said...

thanks for shared wonderful information of giving best information.its more useful and more helpful. great doing keep sharing
SAT Training Centre in Chennai

sindhuja sindhuja said...

Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care and we take your comments to heart.As always, we appreciate your confidence and trust in us
SEO Company in India

malar deepa said...

Great post! I am actually getting ready to across this information, It's very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
Wooden Temple for Home
Tanjore Painting
Pooja Mandir

malar deepa said...

Great post! I am actually getting ready to across this information, It's very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
Wooden Temple for Home
Tanjore Painting
Pooja Mandir

soukya reddy said...

Really Good blog post.provided a helpful information.I hope that you will post more updates like this salesforce Online Training Hyderabad

Nbits said...

thank you for sharing such a good and useful information, please keep on share like this
salesforce training in hyderabad
salesforce online training

Unknown said...

Thanks for sharing this Informative content. Well explained. Got to learn new things from your Blog salesforce online classes

narayana p said...

It's so nice article thank you for sharing a valuable content. google cloud online course

Dharani M said...

Nice information
salesforce training in Marathahalli

salesforce admin training in Marathahalli

salesforce developer training in Marathahalli

asha said...

Nice Post.... Keep on sharing this post
salesforce training in Bangalore

salesforce admin training in Bangalore

salesforce developer training in Bangalore

trustno1 said...

html css examples
Noframes tag noframes element

Post a Comment

 
| ,