I have a js class called Header, which, when the render () method is called, adds the html header code to all pages of the site.
This class, in addition to the render () method, also has a hello () method:
hello() {
console.log('Hello');
}
The question is, how do I add this method as an event to the header button?
I've tried doing it like this:
<button onclick="${this.hello}">Call/button>
But it displays an error in the console: Uncaught SyntaxError: Unexpected token '{'
How to add a class method as an event to a html button?
First, create an object of your class. Then use the object to call the method.
Example:
class Header {
render(){
// render other things
// also render: <button onclick="header.hello();">Call</button>
}
hello() {
console.log('Hello');
}
// other methods here...
}
var header = new Header();
header.render();
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments