Button onClick added through innerHTML doesn't works.
Stencil version:
@stencil/[email protected]
I'm submitting a:
[X] bug report [ ] feature request [ ] support request => Please do not submit support requests here, use one of these channels: https://forum.ionicframework.com/ or https://stencil-worldwide.slack.com
Current behavior:
I have buttons that are dynamically added to an Array coming from my API, these buttons can be clicked to trigger a method in my class and I'm adding these buttons through innerHTML={} attribute. But when i click these buttons they throw an error that my method is not defined or and unexpected token, all depends of what onclick i use.
If i use onClick={(ev) => this.myMethod(ev)} it throws an Uncaught SyntaxError: Unexpected token ).
If i use onclick="myMethod()" it throws Uncaught ReferenceError: sendMessage is not defined.
I know that Angular doesn't let you add those events in innerHTML as explained here, but, as far as i know, in vanilla JS you can add this events in innerHTML since the window has been loaded.
Is it suposed to work like Angular innerHTML or JS innerHTML? Is there a way i can bind a click event to then? Should i use Vanilla JS instead of TSX for this?
Expected behavior:
innerHTML should add these events to my HTML and they should work as expected.
Related code: This is parte of the code i have
export class WabizFaqComponent {
@Prop() name: string;
public html: string =
`<div>
<button onClick={() => this.sendMessage()}>My TSX Click</button>
<button onclick="sendMessage()">My default Button</button>
</div>`;
sendMessage = (ev) => {
console.log('my button click', ev);
};
render() {
return (
<div class="grid">
<div class="col-chat">
<div class="message message-bot" innerHTML={this.html}></div>
</div>
</div>
);
}
}
And this is the output in my HTML:
<div>
<button onclick="{()" ==""> this.sendMessage()}>My TSX Click</button>
<button onclick="sendMessage()">My default Button</button>
</div>
Other information:
(If this doesn't fits as a bug, but instead is a support request please tell me and i'll go to the forum to look for help. Thanks)
Thanks for filing the issue, however, using JSX attributes through innerHTML will not be possible and we will not be supporting that. Instead I would recommend conditionally showing buttons using if statements within your render() function. This way you can dynamically add and remove buttons, and use JSX onClick, and have it all type checked. Thanks for using stencil!