Home > Software engineering >  Why does my Input doesn't fill the value attribute when It is written
Why does my Input doesn't fill the value attribute when It is written

Time:01-05

I have an email input and I'm trying to read the input value but It doen't return a value when It is filled.

<input id="email" name="email" required="" type="email" value=" " />

This is the function I'm trying to execute, saving the Value on a variable to use it later. var email = document.getElementById("email").value;

I have already tried var email = document.getElementById("email"); console.log(email.value);

But have no luck

CodePudding user response:

In this snippet, you can see how you can get the value of the input field as it changes. My suspicion is that you are trying to get the input's value before the user has filled it in (it's a common mistake)

//var ch = 0;
var in1 = document.querySelector("input");
var in2 = document.querySelector("input:last-of-type");
in1.addEventListener("change", function(e) {
//  ch  ;
  console.log("changed: "   in1.value);
});
in2.addEventListener("keyup", function(e) {
  console.log("changed: "   in2.value);
});
<input type="email" />
<input type="email2" />

  • Related