So, how do you properly allocate a function passed as a parameter in a class constructor and then render it to the HTML’s onclick atttribute?
For reference, here’s my code.
class Button {
constructor(id, btnFunction, btnLabel, additionalStyles = ""){
this.id = id;
this.btnFunction = btnFunction;
this.btnLabel = btnLabel;
this.additionalStyles = additionalStyles;
}
renderBtn(){
return `
<button id="${this.id}" onclick="${this.btnFunction}" style="${this.additionalStyles}">${this.btnLabel}</button>
`;
}
}
const locationSet = [
{
name: "startmenu",
buttons: [new Button("start", beginGame, "BEGIN GAME", "display: block; margin: auto;")],
buttonPointers: [document.getElementById("start")],
text: "Welcome to the RPG test game. I'm currently building something in the moment, so just hang tight and enjoy. Take a look around if you want.",
colorPalette: () => {
document.querySelector(':root').style.setProperty('--page-background', '#444');
document.querySelector(':root').style.setProperty('--textbox-color', '#090');
document.querySelector(':root').style.setProperty('--game-text', "#fff");
}
}
];
//Begins Game Function
function beginGame() {
console.log("Working.");
// update(locationSet[1]);
}
If needed, here’s my pen link: https://codepen.io/CasualWanderer20XX/pen/qBzvGJB?editors=0011