I’m trying to get the value of my input by the following simple code but nothing is printed in **result **box.
<body>
<div id="result" style="height:100px"></div>
<hr>
<input type="text" id="inp">
<script>
let inp = document.getElementById('inp');
let result = document.getElementById('result');
result.textContent = inp.value;
</script>
</body>
I was expecting that whatever I enter in the input box, is printed in result box.
2
Answers
Add an event listener for the
"input"
event to get notified each time the value changes.Currently, you are reading the
value
only once, on page load when the user has not entered anything yet.Simply using
oninput
(Whatever I enter in the input box) method: