I try to make a form validator for a inputfield. I try to get the input value from it using the getElementById().value method. But when I console log the variable is empty. I tried to convert my variable into a string, it doesn't solve the problem.
When I type some extra text, the counter next to it increases. But no worthy value showed. How come it's not showing the right input value? thanks!
code of my validator:
const newName = document.getElementById('name');
let newNameValue = document.getElementById('name').value;
let beerArrayNames = this.state.beerArrayName;
newName.addEventListener('input', function(event) {
console.log('test ' + newNameValue);
if (beerArrayNames.includes(newNameValue)) {
newName.setCustomValidity('This already exist!');
} else {
newName.setCustomValidity('');
}
});
<p>Name: <input type='text' id='name' name='name' placeholder='fill in the name' required/> </p>
