Gurnoor Kahlon Gurnoor Kahlon - 25 days ago 8
Javascript Question

I'd like to trigger a function on key and on click

I currently have a function run when the calculate button is clicked.

<button class="hvr-shrink" type="submit" value="submit" id="submit"> Calculate </button>


I want that the function runs when I not only click the submit button but also the ENTER KEY

Function works with perfection just want it to also work when user hits enter

Check the javascript here!

HTML to the function

<html>

<head>

<title>Project</title>

<link href="../CSS files/style.css" rel="stylesheet" type="text/css">
<link href="../CSS files/hover.css" rel="stylesheet" type="text/css">
<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,300,600,700,800' rel='stylesheet' type='text/css'>

</head>

<body>
<div id="home">
<a href="home.html">
<img class="hvr-shadow" src="../../misc/hoem.png" id="homeicon">
</a>
</div>
<h1>Geometric Sequence</h1>
<h2>Find any term of a Geometric Sequence</h2>

<input id="firstTerm" placeholder="First Term?">
<input id="nTerm" placeholder="Nth Term?">
<input id="commonR" placeholder="Common Ratio?">

<button class="hvr-shrink" type="submit" value="submit" id="submit"> Calculate </button>



<p id="term"> </p>

</div>

<script type="text/javascript" src="../math fxn (js)/geoseq.js"></script>
<script src="https://www.gstatic.com/firebasejs/3.6.0/firebase.js"></script>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

</body>



</body>



Answer

Check out the fiddle I have worked on mate and let me know if this is what you need and lets update it to what you want. . . https://jsfiddle.net/e2nf73bu/1/

$(document).on('keypress',function(event){
   if ( event.which == 13 ) {
         $('#submit').click()
  }
})