I'm just starting in javascript and I'm trying to do something very simple, an input and a button, when I click the button, it shows me a value in the console. When entering a specific value in the input, I want that by clicking on the button, the console will show a specific value as well, and if not, it will show another value, simplifying: If the value of the input is X, I want to show Y by clicking on the button. If the value of the input is C, V, Y, etc, I want that when clicking on the button Z is displayed. I leave the code below:
var button = document.getElementById("button")
button = addEventListener("click", function(e){
e.preventDefault
var input = document.getElementById("input").value
if (input == "x"){
console.log("y")
}else {
console.log("z")
}
})
<form action="">
<input type="text" placeholder="number" id="input">
<input type="button" value="enviar" id="button">
</form>
the issue is that when I click on any part of the html or the input itself, it returns the input value, which I do not want, I want it only when, and only when, I click the button, there execute the said function. thanks!