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

            "tag": "div",
            "HTMLAttributes":{"id": "filter-"+ component.get('v.index') +"", "Text":"Test"},
        function(result, status, errorMessage){
             if (status === "SUCCESS") {
                                    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? Nov 24, 2017 at 5:04
  • 1
    I think problem in creating html tag , refer this link salesforce.stackexchange.com/questions/122250/…
    Nov 24, 2017 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.. Nov 24, 2017 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. Nov 24, 2017 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. Nov 24, 2017 at 5:24

1 Answer 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);
  • I found the solution but thats not the direct or simple one. Nov 29, 2017 at 7:20

Not the answer you're looking for? Browse other questions tagged .