Error when accessing element offsetLeft

Question: I am currently trying to make a game where you can make your own circuit boards using logic gates, and whenever I try to drag one of the logic gates, I get this error that says: uncaught TypeError: Cannot read properties of undefined (reading ‘offsetLeft’) at line 181, column 143. Is there a way to fix this? Sorry for the spaghetti code and lack of many comments.

Repl link: Circuit Board Creator

All I can say after a quick look is, errors occur here:

    gateObject[].inputs.forEach((node, index) => {
      node.x = document.getElementById( + document.getElementById(`input${index + 1}`)[0].offsetLeft;
      node.y = document.getElementById( + document.getElementById(`input${index + 1}`)[0].offsetTop;
    gateObject[].outputs.forEach((node, index) => {
      node.x = document.getElementById( + document.getElementById(`output${index + 1}`)[0].offsetLeft;
      node.y = document.getElementById( + document.getElementById(`output${index + 1}`)[0].offsetTop;

document.getElementById( doesn’t contain children elements with classes that start with input1/output1.


Thanks. I realized that I accidentally removed the input1/input2/output1 from the element’s class lists

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.