I have an input and a button. When I clicked the button I want to take the value from input and put it into a variable. How can do this action several times without refreshing the page ? Example: Input: John Button: click let x = John -->without refreshing the page Input: Bob Button: click let x = Bob
You need a variable to store the value of the input and a function that is being called when the button is clicked. name in this example holds the value of the input and is updated everytime the button is clicked.
let name;
document.querySelector("#btn").addEventListener("click", () => {
name = document.querySelector("#name").value;
console.log(name);
});
<input id="name" />
<button id="btn">Click me</button>