Ask coding questions

← Back to all posts
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 (:

Answered by eankeen (2245) [earned 5 cycles]
View Answer
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

caseabj32qj (1)

Thanks so much. Also if I were to make a subscription system how would I do that, so they would enter they're email and then they would subscribe. But somebody could not enter the same email multiple times

eankeen (2245)

@caseabj32qj yah! so to do that, you would have to write your app using a nodejs repl (if you prefer to write in javascript) instead of an 'html, css, js' repl.

your first step would be to create a server using nodejs. with that server, you could

  • server an html page with a form
  • add an option for people to register (enter their email) in the form
  • when people submit the form, the nodejs server gets that data, and stores it in a location*
  • after the nodejs server gets the data, it serves a different html page (ex. a success page, or an 'email already used' page)

*where you store that information is up to you, but it can create extra complexity. for example, you may want to consider storing that information in a file (like a JSON file). that way, when you get a form, you can read the json file, and check if an email already exists. if you want to use a more complex storage solution, you might want to check into databases (although this may be too complex for your case). using a database may better secure personal data, since it wont be in a file.