title: Why is the variable holding the input value always logged as empty?
body: <p>When I <code>console.log</code> the variable that holds the input value, it logs an empty string. The same thing happens with <code>alert</code>. You can test the code in the console and you will see that the output is really there, but empty.</p>
  <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>(function() {
    var button = document.getElementsByTagName("button");
    var userInput = document.getElementById("user_input").value;
  
    button[0].addEventListener("click", function() {
      console.log(userInput);
    }, false);
  })();</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;form&gt;
    &lt;input id="user_input" type="text" placeholder="add new task"&gt;
    &lt;button type="button"&gt;Add&lt;/button&gt;
  &lt;/form&gt;</code></pre>
  </div>
  </div>
  </p>
  <p>Screenshot:</p>
  <p><a href="https://i.stack.imgur.com/vJaR0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vJaR0.png" alt="enter image description here" /></a></p>