plnkr / feedback

Feedback on Plunker
19 stars 11 forks source link

getBoundingClientRect() returns zero values #585

Open 8Observer8 opened 1 year ago

8Observer8 commented 1 year ago

Why does getBoundingClientRect() return 0 values in init() and normal values when I click the button?

https://plnkr.co/edit/b0X2wlNrIvSwYQ9H

<!DOCTYPE html>

<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Example</title>
</head>

<body>
    <button id="startButton">Start Game</button>

    <script type="module">
        let startButton;

        function onStartButtonClick() {
            console.log("Start game");
            console.log(startButton.getBoundingClientRect());
        }

        function init() {
            startButton = document.getElementById("startButton");
            startButton.onclick = onStartButtonClick;
            console.log(startButton.getBoundingClientRect());
        }

        document.addEventListener('DOMContentLoaded', (event) => { init(); });
        //window.onload = () => init();
    </script>
</body>

</html>