-1

My mind has blown. I have a code wich looks (exactly) like this

const A={
    
    B:document.getElementById("plus")
    
}

A.B.addEventListener("click",()=>{
    console.log("1")
})

When i run it, it says "A.B" is null. I tried different versions, but still nothing. Then I copied it right into console and it worked just as expected. I put some console.log statements between object and event listener, and indeed, A and A.B both are null. Though in console they were not. What am I doing wrong?

EDIT: HTML

<!DOCTYPE html >
<html>

<head>
   //some other staff
    <script src="main.js"></script>
</head>
<body>
    <button type="button" id="plus">+</button>
</body>
VVS232
  • 43
  • 5

1 Answers1

0

Oh man.. I got it. Should include my js file after all the DOM is loaded... Such a silly mistake after more than 4 months of practice. Laughing at myself. Thanks

VVS232
  • 43
  • 5