I'm getting a problem where I cannot access the national variables of a Class inside the onclick function of another variable of the same Class. I have made a small demo where I assign a message and return a button which should show the message, but it keeps printing undefined because it can't find the variable. Please Help
class class1 {
message = "";
clicker = document.createElement("button");
constructor(sentence) {
this.message = sentence;
this.clicker.id = "clicker";
this.clicker.innerHTML = "CLICK ME";
this.clicker.addEventListener("click", (function () {
console.log(this.message);
}).bind(this))
return this.clicker;
}
}
document.getElementById("content").appendChild(new class1("hello"));
<html>
<head>
<title></title>
</head>
<body>
<div id='content'></div>
</body>
</html>