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

What is causing a one keypress delay?

When I type something it gets delayed by one keypress. for example if I type h, it would be blank, if i type helloworld it would be helloworl. If I have sad displayed, I would have to press backspace twice to get to sa instead of once. Why is there this delay? How do I fix it?

HTML:

<input type="text" id = "new"></input>
<p id = "result"></p>
<script src = "script.js" type = "module"></script>

javascript:

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

var search = document.getElementById("new");
var resultbox = document.getElementById("result");
search.addEventListener("keydown", function(){
  resultbox.innerHTML = search.value;
})

>Solution :

keydown or keypress is execed before the new character is added to the value of the element.

you can use input or keyup event.

search.addEventListener("input", function(){
  resultbox.innerHTML = search.value;
})
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