That block appears with a class that is passed through input.
The problem is that when you erase a number, the Uncaught TypeError: Cannot read property 'classList' of undefined error appears in the console.
I do not understand why, I wrote the condition, if there is no such element in the array, then nothing needs to be done
<style> .hidden { display: none; } .img { width: 200px; height: 200px; } .img1 {background: green;} .img2 {background: blue;} .img3 {background: red;} </style> <input type="text" class="input"> <div class="img img1 hidden"></div> <div class="img img2 hidden"></div> <div class="img img3 hidden"></div> <script> var input = document.querySelector('.input'); var img = document.querySelectorAll('.img'); var arr = []; img.forEach(function(el, i) { arr.push(i); }) input.oninput = function() { img.forEach(function(element, i) { if(arr.indexOf(Number(input.value)) != -1) { img[input.value - 1].classList.remove('hidden'); } else{ img[i].classList.add('hidden'); } }) } </script>