How to add a class method as an event to a html button

Sag

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?

Ar Rakin

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to call a method from another class in a button click event?

From Dev

How to change the class of an html button at runtime on its click event?

From Dev

How to add event listener with function callback in html inside a class?

From Dev

How to add an `onclick` event for this button

From Dev

how to add mouse event for a button?

From Java

How to add a JButton Button in class

From Dev

How to add button to a <div class>

From Java

how to add button click event in android studio

From Dev

How to add a button with click event on UITableViewCell in Swift?

From Dev

Jquery - how to add a click event for each button

From Dev

How to add an item to a listview on a button click event?

From Dev

How to add a event to a button using Razor?

From Dev

how to code to button event from new class?

From Java

How to add description to method or class

From Dev

How to add parametr to method of class?

From Dev

How to add icon in button html?

From Dev

How to add a Button using HTML?

From Dev

Cannot add a method to a button click event C# UWP

From Dev

jQuery - Can't add a class to a button with an onClick event

From Dev

How to define event handler as elements class method?

From Dev

How to set the class method as event handler?

From Dev

How to call method in class throw event?

From Dev

Add event on inner class by mouseovering on outer class in html using javascript

From Dev

HTML, on click event, how to get the button text

From Dev

how to create button dynamically and add click event to button in android

From Dev

How to add class to the clicked button and remove a class to the previously clicked button

From Dev

How to add a css class on click event in JavaScript?

From Dev

How to add mulitiple event linsteners in GestureDetector class?

From Dev

Calling a typescript method from your html button onclick event

Related Related

  1. 1

    How to call a method from another class in a button click event?

  2. 2

    How to change the class of an html button at runtime on its click event?

  3. 3

    How to add event listener with function callback in html inside a class?

  4. 4

    How to add an `onclick` event for this button

  5. 5

    how to add mouse event for a button?

  6. 6

    How to add a JButton Button in class

  7. 7

    How to add button to a <div class>

  8. 8

    how to add button click event in android studio

  9. 9

    How to add a button with click event on UITableViewCell in Swift?

  10. 10

    Jquery - how to add a click event for each button

  11. 11

    How to add an item to a listview on a button click event?

  12. 12

    How to add a event to a button using Razor?

  13. 13

    how to code to button event from new class?

  14. 14

    How to add description to method or class

  15. 15

    How to add parametr to method of class?

  16. 16

    How to add icon in button html?

  17. 17

    How to add a Button using HTML?

  18. 18

    Cannot add a method to a button click event C# UWP

  19. 19

    jQuery - Can't add a class to a button with an onClick event

  20. 20

    How to define event handler as elements class method?

  21. 21

    How to set the class method as event handler?

  22. 22

    How to call method in class throw event?

  23. 23

    Add event on inner class by mouseovering on outer class in html using javascript

  24. 24

    HTML, on click event, how to get the button text

  25. 25

    how to create button dynamically and add click event to button in android

  26. 26

    How to add class to the clicked button and remove a class to the previously clicked button

  27. 27

    How to add a css class on click event in JavaScript?

  28. 28

    How to add mulitiple event linsteners in GestureDetector class?

  29. 29

    Calling a typescript method from your html button onclick event

HotTag

Archive