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
riz wanriz wan 

How to get Id of lightning:input in recordEditForm

I am dynamically trying to create lightning:recordEditForm by passing record Id, object name, record type id and then rendering fields using iterator.
Because of aura:iteration, all lightning:inputs have same onchange method
I want to find the id of the lightning:input which caused the onchange method to fire.

Code :
 
<lightning:recordEditForm onsubmit="{!c.beforeSave}"
                                  onsuccess="{!c.afterSave}"
                                  recordId="{!v.obj_id}"
                                  recordTypeId="{!v.rec_type_id}"
                                  objectApiName="{!v.obj_name}">
            <lightning:messages />
            <aura:iteration items="{!v.form_field_list}" var="field">
                <lightning:inputField aura:id="{!field.field_api}"
                                      fieldName="{!field.field_api}"
                                      onchange="{!c.handleInputChange}"/>
            </aura:iteration>
            <div class="slds-m-top_medium">
                <lightning:button variant="brand" 
                                  type="submit" name="save" 
                                  label="Save"/>
            </div>
        </lightning:recordEditForm>

 
Best Answer chosen by riz wan
MKRMKR
Hi,

Right. aura:id takes the expression literally and cannot be set dynamically. Try using the fieldName attribute instead:
handleInputChange : function(component, event, helper){
    var fieldName = event.getSource().get('v.fieldName');
    console.log(fieldName);
}

Regards,
Mkr

All Answers

riz wanriz wan
handleInputChange : function(component, event, helper){
        console.log(JSON.stringify(event.getParams()));
        //event.getParams() -> only gives {value : <data in input field>}
       // I want to know which field triggered this event and do different calculations/validations based on field that triggered this event
    }

 
MKRMKR
Hi,

You can access the aura:id attribute with following code:
 
handleInputChange : function(component, event, helper){
    var auraId = event.getSource().getLocalId();
    console.log(auraId);
}

 
Deepali KulshresthaDeepali Kulshrestha
Hi Riz,

- I read your problem and implemented it in my Org and it is working fine.
- Please use the below code 
 
handleInputChange : function(component, event, helper){
       var target = event.getSource();
       var auraId = target .get("v.aura:Id");
       console.log('auraId '+auraId );
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
riz wanriz wan
@Mkr
your approach give me this value in console -> {!field.field_api}
I want the id that's set by the attribute. say field_api = Type__c -> then I want to get that user changed 'Type__c' field

@Deepali
your approach gives me "undefined" in console

can you both share screenshot, in case you people are getting values
MKRMKR
Hi,

Right. aura:id takes the expression literally and cannot be set dynamically. Try using the fieldName attribute instead:
handleInputChange : function(component, event, helper){
    var fieldName = event.getSource().get('v.fieldName');
    console.log(fieldName);
}

Regards,
Mkr
This was selected as the best answer
riz wanriz wan
Thanks Mkr, this works.
Earlier I was using class attribute -> event.getSource().get('v.class') and assign field_api in class.
But fieldName seems better alternative.