I have a let: valorImput, coming from an e.target.value from input. I use this value to compare it with a value in an array and it works. But when I want to get its value it said undefined and I'm not able to use it in future functions. This is a link to Codesand with the whole code.
let valorImput = "";
dropDown.onchange = (e) => {
valorImput = e.target.value;
console.log("dropdown onchange console", e.target.value);
};
btn.onclick = (e) => {
const existe = productos.find((x) => x.titulo === valorImput);
existe ? (precio = existe.precio) : console.log("NO hay Nada");
document.getElementById("valorDeSelect").value = precio;
Swal.fire("El precio esta expresado en Dolares!", "USD " + precio, "success");
console.log("valor input", valorImput);
};
Variable valorImput must be declared in global scope (outside of any "{}" brackets).
See the documentation for more detail.