Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

input changes getting previous value

I’m getting the previous value in console when i write in the input, how can i get the actual value?

const input1 = document.querySelector('#a')

input1.addEventListener('keypress', (e) => {
    console.log(e.target.value)
})
<input type="text" id="a" />

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try eventListener input. Keydown is fired before the character is added to the field, which is why you don’t see the 4 after typing 1234. (An if you prevent the default action of keydown, the character is never added.) keyup is also fired after the character is added.

const input1 = document.querySelector('#a')

input1.addEventListener('input', (e) => {
    console.log(e.target.value)
})
<input type="text" id="a" />
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading