-1

So, I have this sample code:

<textarea id="input" onkeydown="pressed()"></textarea>
<div id="output"></div>
<script>
function pressed()
{
    var input = document.getElementById('input').value;
    document.getElementById('output').innerHTML = input;
}
</script>

My problem is, if type something on the input, the script wont write it on the output immediately, it only does after I type another key. So, is there anything that im not doing right?? Help pls

dummycode
  • 80
  • 5

2 Answers2

0

Use the input event - then paste will also work

document.getElementById("input").addEventListener("input", function() {
  document.getElementById('output').innerHTML = this.value;
})
<textarea id="input" ></textarea>
<div id="output"></div>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
0

Because you're using onkeydown which triggers function pressed immediately, and input will equal to the value of input before you press.

Use onkeyup and it will work fine.

agentp
  • 335
  • 4
  • 17