Появляется ошибка: Uncaught TypeError: Cannot read property 'style' of null

javaScript

function myBoi2() {
        var x = document.getElementById('Button_${i}')
        for (let i = 0; i < 3; i++ )
      if (x.style.display === 'none') {
        x.style.display = 'block';
      } else {
        x.style.display = 'none';
      }
    }

HTML

<div id="Button_1" class="video-container"><iframe src="cilka" width="640" height="390" frameborder="0" allowfullscreen></iframe></div>
</div>

<button class="video-button" onclick="myBoi2()">Тыц</button>

Появляется ошибка: Uncaught TypeError: Cannot read property 'style' of null


Ответы (1 шт):

Автор решения: Павел Ериков

Наверно как-то так:

function myBoi2() {
    for (let i = 0; i < 1; i++ ){
        var x = document.getElementById("Button_"+(i + 1));
        if (x.style.display === 'none') {
            x.style.display = 'block';
        } else {
            x.style.display = 'none';
        }
    }
}
<button class="video-button" onclick="myBoi2()">Тыц</button>

<div id="Button_1" class="video-container" style="background: red;"><iframe src="cilka" width="640" height="390" frameborder="0" allowfullscreen></iframe></div>

Но вы вообще не описали свою проблему, так что первое что пришло в голову.

→ Ссылка