How to get Button Label as message using Lightning Component

By | November 13, 2019

In this scenario we have shown, getting the button label as a message using Lightning Component…

Lightning Component : Basic1

<aura:component >
    <aura:attribute name="Result" type="string"/>
    <lightning:button label="Submit" onclick="{!c.sub}"/>
    <lightning:button label="Cancel" onclick="{!c.can}"/>
    <div>{!v.Result}</div>
</aura:component>

Controller Js

({
	sub : function(component, event, helper) {
		var b1=event.getSource('').get('v.label');
        var msg="Hi you pressed on "+b1;
        component.set('v.Result',msg);
	},
    can : function(component, event, helper) {
		var b=event.getSource('').get('v.label');
        var msg="Hi you pressed on "+b;
        component.set('v.Result',msg);

	}
})

Application

<aura:application extends="force:slds" >
    <c:Basic1/>
</aura:application>

Output

If you click on submit, message will be displayed as shown in image…
Share this...
Share on Facebook
Facebook
Email this to someone
email

Leave a Reply

Your email address will not be published. Required fields are marked *