title: Javascript code only works when debugging
body: <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var selectedLetter = "global right now";
  
  function GetLink() {
    fetch("https://random-word-api.herokuapp.com/word")
      .then(
        r =&gt; {
          if (r.status != 200)
            return;
          r.json().then(
            c =&gt; {
              for (let i = 0; i &lt; c[0].length; i++) {
                p = document.createElement("div");
                p.innerHTML = "_";
                if (i == 0) {
                  p.setAttribute("id", "first");
                }
                p.classList.add("word");
                document.querySelector("#word").appendChild(p)
              }
  
            });
        }
      )
  }
  
  function SetupKeyboard() {
    letters = document.getElementsByClassName("word");
    selectedLetter = document.getElementById("first");
  }
  
  window.addEventListener('load', () =&gt; {
    GetLink();
    SetupKeyboard();
  });</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>html,
  body,
  #wrapper {
    height: 100%;
    width: 100%;
  }
  
  #title {
    font-size: xx-large;
    text-align: center;
  }
  
  #word {
    font-size: xx-large;
    text-align: center;
  }
  
  .word {
    display: inline;
    margin: 2px;
    background-color: beige;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  
  &lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&gt;
    &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
    &lt;title&gt;Hangman&lt;/title&gt;
    &lt;link rel="stylesheet" href="style.css"&gt;
    &lt;script src="index.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
    &lt;div id="title"&gt;
      Hangman
    &lt;/div&gt;
    &lt;div id="word"&gt;
  
    &lt;/div&gt;
  &lt;/body&gt;
  
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>
  <p>It seems to be a simple problem but it's driving me insane.</p>
  <pre><code>function SetupKeyboard()
  {
    letters=document.getElementsByClassName(&quot;word&quot;);    
    selectedLetter=document.getElementById(&quot;first&quot;);
  }
  </code></pre>
  <p>I need the selected letter to be the first element of the &quot;letters&quot; array but for some reason <code>selectedLetter=letters[0]</code> gave back &quot;undefined&quot;. I tried this approach too and it's the same problem. I am obviously missing something.</p>
  <p>When I debug the code it works properly and the console logs everything as expected. When NOT in debugger the value of selectedLetter is &quot;undefined&quot;. Also, the problem is gone when I call the function manually through browser console as well.</p>
  <p>I know the problem is not in the DOM not existing because I run it all like this:</p>
  <pre><code>window.addEventListener('load', ()=&gt;{
      GetLink();
      SetupKeyboard();
  });
  </code></pre>