I'm trying to have a button that submits a text input, and have that input show as an alert. Here's my code so far (which shows an alert with "undefined"):
<p class="text-right">
<input id="input" type="text" placeholder= "Your Name">
<button id="submit" type="submit"> Submit</button>
</p>
And my Javascript code:
var input = document.getElementById("input").value;
var submit = document.getElementById("submit");
submit.addEventListener("click", function1);
function function1() {
alert(input.value);
}
var submit = document.getElementById("submit");
submit.addEventListener("click", function1);
function function1() {
var input = document.getElementById("input").value;
alert(input);
}
You're alerting input.value.value
, but you already have the value in the input variable. EDIT :
var submit = document.getElementById("submit");
var input = document.getElementById("input");
submit.addEventListener("click", function1);
function function1() {
alert(input.value);
}
Which is declaring the input once, and each time the function is called, you alert it's value.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.