I'm trying to console.log the value submitted by a form but getting "uncaught TypeError cannot read properties of undefined" Error. I'm not sure why.
HTML
<form action="" id="p2Input">
<input type="text" name="" id="guess" placeholder="Guess a letter">
<input type="submit" value="Guess">
</form>
JavaScript
function processGuess(event){
event.preventDefault(); //this is where the error shows
let guess = document.getElementById("guess").addEventListener("Submit",null);
console.log(guess);
}
You should call processGuess when form has submitted, you can do it, by defining onsubmit on your form, like this:
function processGuess(event){
event.preventDefault();
let guess = document.getElementById("guess").value;
console.log(guess);
}
<form action="" id="p2Input" onsubmit="processGuess(event)">
<input type="text" name="" id="guess" placeholder="Guess a letter">
<input type="submit" value="Guess">
</form>