I want to call level(lvl) function with arguments 1, 2 or 3 are passed with respective buttons with id's one, two and three. However, whenever the page loads, the third option is already executed without any clicking. What am I missing, is this not the right way to do this?
Here is the javascriptcode.
const level = function(lvl) {
if(lvl === 1) {
ctx.canvas.width = 400;
ctx.canvas.height = 400;
cols = 9;
rows = 9;
numbombs = 10;
console.log("called 1");
return;
}
if(lvl === 2) {
ctx.canvas.width = 490;
ctx.canvas.height = 490;
cols = 13;
rows = 13;
numbombs = 30;
console.log("called 2");
return;
}
if(lvl === 3) {
ctx.canvas.width = 1050;
ctx.canvas.height = 490;
cols = 30;
rows = 14;
numbombs = 99;
console.log("called 3");
return;
}
};
document.getElementById("one").onclick = level(1);
document.getElementById("two").onclick = level(2);
document.getElementById("three").onclick = level(3);
And the html part
<button id="one" class="lvl">Beginner</button>
<button id="two" class="lvl">Intermediate</button>
<button id="three" class="lvl">Advanced</button>