9

How can i get the aura:id dynamically from event attribute, the below example i was trying to get the aura:id to get the value, else you can think as aura:id might be some field APIname so i can pass this field name and value to controller to store the value dynamically.

code component:

<aura:component controller = "LC_AutoSave" implements="force:appHostable" >
    <form>
        <ui:inputtext  value = ""  aura:id ="testtext"  blur = "{!c.autoSave}"/>
        <ui:inputtext value = "" aura:id ="testtext1"  blur = "{!c.autoSave}"/>
        <ui:inputtext value = "" aura:id ="testtext2"  blur = "{!c.autoSave}"/>

   </form>
</aura:component>

Controller

({
    autoSave : function(component, event, helper) {
        var a = event.getSource();
        console.log('Test'+a.get("v.aura:Id"));
        helper.autoSavehelper(component,event);
    }
})
10

I'm pretty sure you just call component.getLocalId()

So it'd be:

var a = event.getSource();
var id = a.getLocalId();

To find the local ID for a component in JavaScript, use cmp.getLocalId().

Source here:

| improve this answer | |
  • It works to me ,Thanks, also please suggest how could i find these methods information, i searched aura framework documentation didn't find it. – Raj_SFDC Sep 13 '16 at 19:58
  • That's a tricky one. Sometimes you just need to know what to search for. That will come with experience. I'll update the question with the link. – Caspar Harmer Sep 13 '16 at 20:04
  • This is great however, i notice that it works for me as a static value, eg: id="tehidval" however, if i use it like id="{!v.attribute}" becuase this is dynamic generated by an action, then it will display {!v.attribute}, I am wondering if this is possible? – manza Oct 30 '18 at 20:16
  • hmm, it should work dynamically - because this is how you specify a unique id for the component - ie id="{!GlobalId + 'some_val'}" ... so replacing that with an attribute I would have thought would work. – Caspar Harmer Oct 30 '18 at 21:06

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.