title: How can I execute a function on pressing the enter key in an <input> field?
body: <p>I have an input and I'd simply like to add an event listener for it to activate a function when I press enter, when the input is focused. How do I do this with pure JS?</p>
  <p>Right now I have:</p>
  <p>HTML:</p>
  <pre class="lang-html prettyprint-override"><code>Enter your wage:&lt;input type=&quot;text&quot; id=&quot;wage&quot; value =&quot;&quot; size=20&gt;
  &lt;button id=&quot;sub&quot;&gt;Submit&lt;/button&gt;
  </code></pre>
  <p>JavaScript:</p>
  <pre class="lang-js prettyprint-override"><code>var wage = document.getElementById(&quot;wage&quot;);
  wage.addEventListener(&quot;change&quot;, validate);
  
  var btn = document.getElementById(&quot;sub&quot;);
  btn.addEventListener(&quot;click&quot;, validate);
  </code></pre>
  <p>So basically the function <code>validate()</code> activates when I click OR change the text, but I want to call it by pressing enter.</p>