1

I am able to dynamically create "ui:button" using $A.createComponent as shown in: https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/js_cb_dynamic_cmp_async.htm

However, I am not able to do the same for "button". Can somebody help me why is this happening and also how to render "button".

1 Answer 1

2

you need to use aura:html to create a html tag dynamically using $A.createComponent.

So to create a button tag below is how you do it:

$A.createComponent(
    "aura:html",
    {
        "tag":"button",
        "body":"click me",
        "HTMLAttributes":{
            "id":"dynamicbtn",
            "class":"slds-button slds-button--brand"
        }
    },
    function(newBtn){
        if (cmp.isValid()) {
            var body = cmp.get("v.body");
            body.push(newBtn);
            cmp.set("v.body", cmp);
        }
    }
);
6
  • Thanks for answering, it did render the button the way I wanted!!! One more question here, on click of this button it is giving me error: "[TypeError: event.getSource is not a function]". I have used event.getSource() to get ID of button pressed in controller which it is not accepting I think. Will really appreciate if you would guide on this as well!! Commented Sep 30, 2016 at 11:47
  • event.getSource() works only for ui namespaced component(i.e aura component).you can get the id of the button using event.getAttribute('id'). Commented Sep 30, 2016 at 11:57
  • Ah my bad it's event.target.getAttribute('id') Commented Sep 30, 2016 at 12:14
  • Its not getting 'event' itself in the controller on click, I tried seeing event using alert(event) but it is showing error: 'Something has gone wrong. $A.getDefinition. Please try again.' Sorry for bad editing Commented Sep 30, 2016 at 12:18
  • Ok I'll update the answer which contains click event attached to the button Commented Sep 30, 2016 at 12:21

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.