I am looping through a list of records in a lightning component. I want to get the record Id for the selected checkbox record in the js controller.

<aura:iteration var="sw" items="{!v.swList}">
        <td >
            <div class="slds-truncate" >{!sw.contact.Name}</div>
        <td >
            <div class="slds-truncate">{!sw.contact.Account__c}</div>
        <td >
            <div class="slds-p-left_large">
                <lightning:input type="checkbox" checked="{!sw.contact.Optin__c}" onclick = "{!c.optIn}"/>
  • is your id in your swList ? or some other attribute? – glls Aug 23 '18 at 5:10
  • yes {!sw.contact.Id} is available in the list – sfdcdev Aug 23 '18 at 5:15

in your iteration, you might want to set the id to some attribute and use an onchange event handler, for example:

<lightning:input type="checkbox" checked="{!sw.contact.Optin__c}" value="{!sw.contact.Id}" onclick = "{!c.optIn}"/>

and use event.getSource().get('v.value') in the client-side controller to get the button components value that was triggered the onchange event

  • Thanks glls. I am switching to <ui:inputCheckbox> , can you please let me know how would I do the same with <ui:inputCheckbox value="{!sw.contact.Optin__c}" change = "{!c.optIn}"/> – sfdcdev Aug 29 '18 at 0:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.