function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
koshkosh 

lightning:button get value in client controller

I create a bunch of buttons in a table with aura:iteration. Think of it as one per contact per row to act on the record. Here is the button markup inside a td -
 
<lightning:button variant="brand" label="Charge" value="{!s.Id}" onclick="{!c.callMyCallback}" />
Now, how do I access the value attribute for the button in my client js controller callback? event.currentTarget.value is undefined. Any ideas?
Thanks.
 
Jim JamJim Jam
try ..  event.target.value
koshkosh
No, that doesnt not work. I've also tried event.getSource() but that is undefined as well
David Roberts 4David Roberts 4
see https://developer.salesforce.com/forums/?id=906F0000000kAn0IAE
Ajit Jain 9Ajit Jain 9
I am also haing same doubt..!!!!
 
Khemanshu RaoKhemanshu Rao
Hi Kosh, 
put name attribute in button like this value="{!s.Id}" and then use event.getSource().get("v.name"). You will get the name of button. Please mark this as correct answer if it works for you.
David Roberts 4David Roberts 4
Here are two ways to achieve this (summarised from https://developer.salesforce.com/forums/?id=906F0000000kAn0IAE):

1. Nestor Velázquez's solution:
In component

<aura:attribute name="theFormObject" type="CustomObject__c" default="{ 'sobjectType': 'theObject__c',                 'Field1__c': '', 'Field2__c': '' }"/> 

<form class="slds-form--stacked"> 
<lightning:input aura:id="theForm" label="Field 1: " value="{!v.theFormObject.Field1__c}" /> 
<lightning:input aura:id="theForm" label="Field 2: " value="{!v.theFormObject.Field2__c}" /> 
</form>

In controller 

var data     = component.get("v.theFormObject"); 
var value1 = data.Field1; 
var value2 = data.Field2;



2. Use a wrapper class to hold the contact details and other info so an iterative list can allow selection from the list.
 
In component

<aura:iteration var="varContactWrapper" items="{!v.contactWrapperList}"> 

<lightning:button variant="neutral"  label="Cancel" onclick="{!c.handleCancel}"
       aura:id="{!varContactWrapper}"  name="{!varContactWrapper}">
</lightning:button>     


In controller

handleCancel : function (component, event, helper) {
        var contactToGo = event.getSource().get("v.name");
        console.log('contactToGo '+contactToGo.cont.Name);
        ...etc...
}//handleCancel