-1

I am trying to create a div using $A.createComponent but its throwing error.

`$A.createComponent("aura:HTML",
        { 
            "tag": "div",
            "HTMLAttributes":{"id": "filter-"+ component.get('v.index') +"", "Text":"Test"},
            "class":""
        },
        function(result, status, errorMessage){
             if (status === "SUCCESS") {
                $A.createComponents(components,
                                    function(components, status, errorMessage){
                                         if (status === "SUCCESS") {
                                           var divElement = document.getElementById("filter-" + component.get("v.index"));
                                           //var divElement = component.find(component.get('v.targetDiv'));
                                           console.log('Div element');
                                           var elementBody = divElement.get("v.body");
                                         }
                                     } 
                                    );
                       } 
         }
       );

Please ignore if any missing brackets are there I may have made mistake while copying it. It throws that divElement does not have get method or divElement is undefined. I know that component.find(component.get('v.index')) is trying to assign some value which never exist. how can I over come this situation?

  • No i have not assigned it. let me do that. What difference does it make? Can you please explain me? – Sagar Hinsu Nov 24 '17 at 5:04
  • 1
    I think problem in creating html tag , refer this link salesforce.stackexchange.com/questions/122250/… – NITHESH K Nov 24 '17 at 5:11
  • Thanks for the link but. I want to add my data in the particular div. I already referred the link which you provided. Means I want to push my result in that div.. – Sagar Hinsu Nov 24 '17 at 5:16
  • The div element that you have created is an HTML element and not a lightning or aura element. As a result it does not have a method such as get('v.body'). I believe when the error occurs on this line, the JS stops executing further and the element does not appear on your lighting component. – Mohit Arora Nov 24 '17 at 5:22
  • yes it may be the reason. How can add the result to the div? But in developer tool the divElement it self shows undefined. – Sagar Hinsu Nov 24 '17 at 5:24
1

$A.createComponent does not actually put the element in the DOM anywhere. result in your code is the div that was created, but you still need to put it somewhere.

In your second callback, you could refer to it directly:

result.set("v.body", components);

But don't forget to place it back into your component's body:

component.set("v.body", result);
| improve this answer | |
  • I found the solution but thats not the direct or simple one. – Sagar Hinsu Nov 29 '17 at 7:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.