0

I have an input that I want to track it with each keydown event but at the first time I'm getting empty output and then one of letters repeating itself. try sssd for input , first you will get empty output and at last you will get ssss and I tried on other events too

let theinput = document.getElementById("clrinput");

theinput.addEventListener("keydown", () => {
  console.log(theinput.value);
});
<input type="text" id="clrinput">
IhateReact
  • 43
  • 6

2 Answers2

1

As Barmar pointed out the keydown happens before the value is updated, if you change to keyup it works fine

let theinput = document.getElementById("clrinput");

theinput.addEventListener("keyup", () => {
  console.log(theinput.value);
});
<input type="text" id="clrinput">
LeeLenalee
  • 27,463
  • 6
  • 45
  • 69
1

use input event.. ?

let theinput = document.getElementById('clrinput');

theinput.addEventListener('input', () => {
  console.clear()
  console.log( theinput.value )
});
<input type="text" id="clrinput">
Mister Jojo
  • 20,093
  • 6
  • 21
  • 40