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".
Add a comment
|
1 Answer
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);
}
}
);
-
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!!– SarangCommented 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 usingevent.getAttribute('id')
.– PraveenCommented Sep 30, 2016 at 11:57 -
-
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– SarangCommented Sep 30, 2016 at 12:18
-
Ok I'll update the answer which contains click event attached to the button– PraveenCommented Sep 30, 2016 at 12:21