I spent way too much time on this, but here is what happened to me trying to fix a bug. I have to get value of an <input type="text">
field with id="name"
. Here is what I did:
var name = document.getElementById("name");
console.log(name.value);
I get the response undefined
. I tried a few more stuff and then realized what just happened, with this piece of code for reference.
. . .
console.log(name);
console.log(typeof(name));
And I get the output
"[object HTMLInputElement]"
"string" //why is this???
I only get the output as a string, with value above. I do not understand why it is returning a string instead of the vlaue inside the string. Here is where I solve the bug, I chose a different variable name and the code as follows:
var playerName = document.getElementById('name');
console.log(playerName);
console.log(typeof(playerName));
console.log('\n' + playerName.value);
and get the following output:
<input type="text" placeholder="Enter name" id="name">
object
Hello //this is the real val entered ...and this makes absolutely no sense to me whatsoever..
Just for clarification: I did not run anything before this, I tried this after the problem in a new HTML file with a new <script>
tag. I ran these pieces of code exactly like showed here, and I did it about 3 times and got the same result. If it helps my browser is: Google Chrome version 63.0.3239.132 (Official Build) (64-bit)
.
Thank you :-)