When I console.log
the variable that holds the input value, it logs an empty string. The same thing happens with alert
. You can test the code in the console and you will see that the output is really there, but empty.
(function() {
var button = document.getElementsByTagName("button");
var userInput = document.getElementById("user_input").value;
button[0].addEventListener("click", function() {
console.log(userInput);
}, false);
})();
<form>
<input id="user_input" type="text" placeholder="add new task">
<button type="button">Add</button>
</form>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…