Okay so i have been looking on how to call a method from javascript in an onclick event in for example let's say a button, but i can't find any answers.
This is what I have:
Javascript
class SomeClassName() {
methodName() {
alert("test");
}
}
html
<button type="button" onclick="methodName()">Click me</button>
Obviously I didn't expect this to work since you need to make an instance like:
Object = new SomeClassName();
Object.methodName();
But I can't figure out how to do this on an onclick event.
Don't define your 'class' with parens
class SomeClassName()
additionally, if you want to call a method without instantiating it, you must make that method 'static'
class SomeClassName { static methodName() { alert("test"); } }
<button type="button" onclick="SomeClassName.methodName()">Click me</button>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.