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:
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;
})