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);
            }
        }
    );
  • 1
    you can create dynamically lightning component developer.salesforce.com/docs/atlas.en-us.lightning.meta/… – Ratan Paul May 18 '16 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 – Scott Morrison May 18 '16 at 13:32
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);
          }
     }
);
| improve this answer | |
  • 2
    It works, but my goodness is that ever chatty. – Scott Morrison Sep 29 '16 at 14:58
  • And if you dont want to use jquery? – Kevin Hernandez Mar 28 '19 at 23:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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