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[elmnt.id].inputs.forEach((node, index) => {
      node.x = document.getElementById(elmnt.id).offsetLeft + document.getElementById(elmnt.id).getElementsByClassName(`input${index + 1}`)[0].offsetLeft;
      node.y = document.getElementById(elmnt.id).offsetTop + document.getElementById(elmnt.id).getElementsByClassName(`input${index + 1}`)[0].offsetTop;
    });
    gateObject[elmnt.id].outputs.forEach((node, index) => {
      node.x = document.getElementById(elmnt.id).offsetLeft + document.getElementById(elmnt.id).getElementsByClassName(`output${index + 1}`)[0].offsetLeft;
      node.y = document.getElementById(elmnt.id).offsetTop + document.getElementById(elmnt.id).getElementsByClassName(`output${index + 1}`)[0].offsetTop;
    });

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

2 Likes

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.