8

Since HTML tags are first class citizens in lightning is there a way to instantiate them as components in lightning? Something like:

    $A.createComponent(
        "html:div",
        {
            "aura:id": "test1",
            "textContent": "test"
        },
        function(div){
            //Add the new button to the body array
            var container = cmp.find("container");
            if (container.isValid()) {
                var body = container.get("v.body");
                body.push(div);
                container.set("v.body", body);
            }
        }
    );
2
  • 1
    you can create dynamically lightning component developer.salesforce.com/docs/atlas.en-us.lightning.meta/…
    – Ratan Paul
    May 18, 2016 at 6:10
  • Thanks, I am aware that lighting components can be dynamically created, but I get an internal error if I try and create html elements. The docs say html elements are first class citizens but it doesn't appear that they can be created using createComponent like any other lighting component May 18, 2016 at 13:32

1 Answer 1

12

This worked for me:

$A.createComponent(
    "aura:html",
     { 
         tag: "div",
         HTMLAttributes:{"id": "Temp","class": "class name here"}
     },
     function(compo){
          var container = cmp.find("container");
          if (container.isValid()) {
              var body = container.get("v.body");
              body.push(compo);
              container.set("v.body", body);
          }
     }
);
2
  • 2
    It works, but my goodness is that ever chatty. Sep 29, 2016 at 14:58
  • And if you dont want to use jquery? Mar 28, 2019 at 23:49

You must log in to answer this question.

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