0

<aura:iteration items="{!v.userCustomFieldSet}" var="fieldSet" indexVar="i"> <ui:inputText value="{!fieldSet.FieldValue}" aura:id="{!fieldSet.FieldName}"/> </aura:iteration>

Above code, Getting Error when i'm accessing the field-set Cannot read property 'getElement' of undefined

2 Answers2

0

Like glls pointed out, dynamic aura:ids are not supported (yet). It will be helpful to know what your end goal is so we can make an effective recommendation. Did you know that if you set the aura:id to a static value like: aura:id="myInput", you can get the values in ControllerJS by doing: component.find("myInput").get("v.value")? That will work as long as there is precisely ONE matching aura:id in the component. But in your case there will be multiple because of aura:iteration, so component.find("myInput") will return an array of all matching elements you can easily loop through.

Lightning Evangelist
  • 3,458
  • 3
  • 33
  • 67
0

As mentioned in by @glls in the comment and @Kal answer - You cannot assign auraid’s dynamically, instead you can use the element's id via document.getElementById:

<aura:iteration items="{!v.userCustomFieldSet}" var="fieldSet" indexVar="i"> 
     <ui:inputText value="{!fieldSet.FieldValue}" id="{!fieldSet.FieldName}"/> 
</aura:iteration>

and in you controller get the element like this:

var fieldName = document.getElementById('fieldName');

I got help on this from an answer that @sfdcfox gave me here:

component.find an aura:id item created in aura:iteration

Itai Shmida
  • 5,025
  • 12
  • 59
  • 86