Uncaught TypeError: Cannot set properties of undefined (setting 'display')

subb_ny·2022년 8월 24일
0
function clickButton(k) {
        document.getElementsByClassName("alert").innerText = k;
        document.getElementsByClassName("alert-box").style.display = "block";
      }
function clickButton(k)[0] {
        document.getElementsByClassName("alert").innerText = k;
        document.getElementsByClassName("alert-box")[0].style.display = "block";
      }   

document.getElementsByClassName은 nodeList를 전달한다. 따라서 접근하고자 하는 값의 위치를 명시해줘야 모호하지 않다.
해결방법: 리스트번호를 명시해주면 된다. 위에서는 0번째로 하였지만, 자신이 원하는 위치가 있다면 값을 변경하여 접근할 수 있다.

  

0개의 댓글