caseabj32qj (1)

Hello I am very new to this website, and I was wondering if somebody could tell me how to link a variable to a button. so if the clicked the button it would send a console alert. If somebody can answer this


caseabx32qj (:

You are viewing a single comment. View All
eankeen (2245)


in your html, you can add a button

<button id="myButton">button text</button>

in your javascript, you can add the following

document.getElementById("myButton").addEventListener("click", function(event) {
    console.log("i got clicked");
    console.log("this is the variable X: ", X);

this adds a listener to your button. so that when you click it, it executes the function. the function just logs some text to the console