Search code examples
javascriptsalesforcesalesforce-lightning

Salesforce Aura component, how to pass value from lightning:select to onchange controller?


I have a lightning:select component in my aura component. I need to have the onchange method, 'handleUnitTypeChange', be able to access the {!container.charge.Id} value. I have tried a few different ways of doing this but nothing is working so far. Below is one attempt I made to set it as the aura:id but this does not work as it seems aura:id can only be a hard-coded string value, not a value from a variable. Was wondering if anyone had an idea on this? Thanks for any help.

Additional context: this is within a loop, so {!container.charge.Id} is a variable from looping over a list.

cmp file:

        <lightning:select aura:id="{!container.charge.Id}" value="{!container.charge.Unit_Type__c}" onchange="{!c.handleUnitTypeChange}">
            <option text="Select" value=""/>
            <aura:iteration items="{!v.pickListChargeUnitType}" var="option" indexVar="key">
                <option text="{!option.value}" value="{!option.key}" selected="{!option.key==container.charge.Unit_Type__c}" />
            </aura:iteration>
        </lightning:select>

js:

    handleUnitTypeChange: function (component, event, helper) {


        let localId = event.getSource().getLocalId();

}

Solution

  • I was able to get help from a colleague on this one so posting this in case it's useful to others:

    Solution:

    Use the 'title' attribute to hold the value:

    <lightning:select title="{!container.charge.Id}" value="{!container.charge.Unit_Type__c}" onchange="{!c.handleUnitTypeChange}">
    

    Then, in the js controller get it via:

    let title = event.getSource().get("v.title")