title: Run JavaScript functions when user presses enter
body: <p>I made a calculator with specific properties in HTML and I would like for the user to be able to press enter to show both of <code>display1()</code> and <code>display2()</code>.
  <br>In other words, I want the enter key to trigger button <code>Both</code>.</p>
  
  <p>Here is the code I have and what I have tried so far I attempted to add:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;Square Yards Calculator&lt;/title&gt;
      &lt;/head&gt;
  
      &lt;body&gt;
          &lt;div id='calc-contain'&gt;
  
            &lt;form name="calculator"&gt;
  
              &lt;input type="text" name="answer" /&gt;
              &lt;br&gt;
  
              &lt;input type="button" value=" 1 " onclick="calculator.answer.value += '1'" /&gt;
              &lt;input type="button" value=" 2 " onclick="calculator.answer.value += '2'" /&gt;
              &lt;input type="button" value=" 3 " onclick="calculator.answer.value += '3'" /&gt;
              &lt;input type="button" value=" + " onclick="calculator.answer.value += '+'" /&gt;
              &lt;br/&gt;
  
              &lt;input type="button" value=" 4 " onclick="calculator.answer.value += '4'" /&gt;
              &lt;input type="button" value=" 5 " onclick="calculator.answer.value += '5'" /&gt;
              &lt;input type="button" value=" 6 " onclick="calculator.answer.value += '6'" /&gt;
              &lt;input type="button" value=" - " onclick="calculator.answer.value += '-'" /&gt;
              &lt;/br&gt;
  
              &lt;input type="button" value=" 7 " onclick="calculator.answer.value += '7'" /&gt;
              &lt;input type="button" value=" 8 " onclick="calculator.answer.value += '8'" /&gt;
              &lt;input type="button" value=" 9 " onclick="calculator.answer.value += '9'" /&gt;
              &lt;input type="button" value=" Flex61 "   onclick="display1()" /&gt;
              &lt;/br&gt;
  
              &lt;input type="button" value=" c " onclick="calculator.answer.value = '',
                                  document.getElementById('printhere1').innerHTML= '',
                                  document.getElementById('printhere2').innerHTML= ''" /&gt;
  
              &lt;input type="button" value=" 0 " onclick="calculator.answer.value += '0'" /&gt;
              &lt;input type="button" value=" Both " onclick="display1(),display2()" /&gt;
              &lt;input type="button" value=" Gap55 "    onclick="display2()" /&gt;
              &lt;/br&gt;
  
  
            &lt;/form&gt;
            &lt;div id="agh"&gt;
              &lt;p&gt;Enter how many Square yards (SY) you are covering&lt;/p&gt;
              &lt;p id="printhere1"&gt;&lt;/p&gt;
              &lt;p id="printhere2"&gt;&lt;/p&gt;
            &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;script type="text/javascript"&gt;
            function display1(){
              //Assigning the variable to the user input
              var squareyards = eval(calculator.answer.value);
              if (squareyards &lt; 0 || squareyards == null){
                  squareyards = 0
              }
              var pounds = 5
              pounds = squareyards * pounds
              // to print the input here
              document.getElementById("printhere1").innerHTML = "For "+ squareyards + " SY sou need: "+ pounds + " lbs of Elasto Flex61";
              }
  
              function display2(){
                  //Assigning the variable to the user input
                  var squareyards = eval(calculator.answer.value);
                  if (squareyards &lt; 0 || squareyards == null){
                      squareyards = 0
                  }
                  var rolls = 9
                  rolls = Math.ceil(squareyards * rolls / 103)
                  // to print the input here
                  document.getElementById("printhere2").innerHTML = "For "+ squareyards + " SY sou need: "+ rolls + " Rolls of Gap55 Smooth";
              }
              $(function(){  
                  $(':text').bind('keydown',function(e){ //on keydown for all textboxes  
                      if(e.keyCode==13){ //if this is enter key  
                          e.preventDefault();
                          e.display1();
                          e.display2();
                      }             
                  });               
              });  
  
          &lt;/script&gt;
  
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>