Skip to main content
Post Closed as "Duplicate" by Himanshu, Raul, glls, Ratan Paul, battery.cord
added 547 characters in body
Source Link
Kiruthika
  • 13
  • 1
  • 2
  • 8

When I click on lightning:button(Create), I am able to dynamically create lightning:input element from js controller but When I click on lightning:button(Set Value), I am not able to set value for the lightning:input by accessing its aura:id. I am getting error as " [Cannot read property 'set' of undefined]"...Even I tried the answer posted in(Aura:Id of dynamically created compontns cannot be accessed) but there they have used ui:button but here I am using lightning:input.

createElements : function(component, event, helper){
         $A.createComponent(
                "lightning:input",
                {
                    "aura:id":"objectField",
                    "label": "User Name",
                    "type": "text"
                },
                function(msgBox){                
                    if (component.isValid()) {
                        var targetCmp = component.find('createdynamicInputText');
                        var body = targetCmp.get("v.body");
                        body.push(msgBox);
                        targetCmp.set("v.body", body); 
                    }
               }
           ); 
    },
    setValue : function(component, event, helper){
        component.find("objectField").set("v.value","Testing");
    }

I have also tried the below code but not working for my thing,

setValue : function(component, event, helper){
        //component.find("objectField").set("v.value","Testing");
        var input = component.find({ instancesOf : "lightning:input" })[0];
        input.set("v.value", "Testing");
    } 

When I click on lightning:button(Create), I am able to dynamically create lightning:input element from js controller but When I click on lightning:button(Set Value), I am not able to set value for the lightning:input by accessing its aura:id. I am getting error as " [Cannot read property 'set' of undefined]"

createElements : function(component, event, helper){
         $A.createComponent(
                "lightning:input",
                {
                    "aura:id":"objectField",
                    "label": "User Name",
                    "type": "text"
                },
                function(msgBox){                
                    if (component.isValid()) {
                        var targetCmp = component.find('createdynamicInputText');
                        var body = targetCmp.get("v.body");
                        body.push(msgBox);
                        targetCmp.set("v.body", body); 
                    }
               }
           ); 
    },
    setValue : function(component, event, helper){
        component.find("objectField").set("v.value","Testing");
    } 

When I click on lightning:button(Create), I am able to dynamically create lightning:input element from js controller but When I click on lightning:button(Set Value), I am not able to set value for the lightning:input by accessing its aura:id. I am getting error as " [Cannot read property 'set' of undefined]"...Even I tried the answer posted in(Aura:Id of dynamically created compontns cannot be accessed) but there they have used ui:button but here I am using lightning:input.

createElements : function(component, event, helper){
         $A.createComponent(
                "lightning:input",
                {
                    "aura:id":"objectField",
                    "label": "User Name",
                    "type": "text"
                },
                function(msgBox){                
                    if (component.isValid()) {
                        var targetCmp = component.find('createdynamicInputText');
                        var body = targetCmp.get("v.body");
                        body.push(msgBox);
                        targetCmp.set("v.body", body); 
                    }
               }
           ); 
    },
    setValue : function(component, event, helper){
        component.find("objectField").set("v.value","Testing");
    }

I have also tried the below code but not working for my thing,

setValue : function(component, event, helper){
        //component.find("objectField").set("v.value","Testing");
        var input = component.find({ instancesOf : "lightning:input" })[0];
        input.set("v.value", "Testing");
    } 
Source Link
Kiruthika
  • 13
  • 1
  • 2
  • 8

cannot access aura:id of dynamically created Lightning Element

When I click on lightning:button(Create), I am able to dynamically create lightning:input element from js controller but When I click on lightning:button(Set Value), I am not able to set value for the lightning:input by accessing its aura:id. I am getting error as " [Cannot read property 'set' of undefined]"

<aura:component>
     <lightning:button label="Create" onclick="{!c.createElements}" variant="brand"/> <br/>
     <div aura:id="createdynamicInputText"></div>
     <lightning:button label="Set Value" onclick="{!c.setValue}" variant="brand"/> <br/>
</aura:component>

js controller

createElements : function(component, event, helper){
         $A.createComponent(
                "lightning:input",
                {
                    "aura:id":"objectField",
                    "label": "User Name",
                    "type": "text"
                },
                function(msgBox){                
                    if (component.isValid()) {
                        var targetCmp = component.find('createdynamicInputText');
                        var body = targetCmp.get("v.body");
                        body.push(msgBox);
                        targetCmp.set("v.body", body); 
                    }
               }
           ); 
    },
    setValue : function(component, event, helper){
        component.find("objectField").set("v.value","Testing");
    } 

can anyone please help me here to solve this issue, thanks in advance