-1

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>

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 1
    please organize your javascript code a little better so we can have a better view. Thanks – HenryDev Nov 14 '16 at 00:59
  • @Gurnnor Kahlon where is the user suppose to give an input?? you don't have an input field?? – HenryDev Nov 14 '16 at 01:01
  • @HenryDev I fixed it! – Gurnoor Kahlon Nov 14 '16 at 01:03
  • Do you only have a button in html?? you don't have an input field where user can enter data in? – HenryDev Nov 14 '16 at 01:05
  • @Gurmoor Kahlon I see your javascript code. But what else do you have in the HTML?? only the button?? – HenryDev Nov 14 '16 at 01:07
  • your html code is a picture. Can you please paste it on here so its easy to copy and start working on it. Thanks! – HenryDev Nov 14 '16 at 01:09
  • @HenryDev I just need a suggestion on how to make it run on both the on-click and on the onkeypress – Gurnoor Kahlon Nov 14 '16 at 01:10
  • @Gunoor its hard to test it without actually working on the code. Please paste hmtl code on here – HenryDev Nov 14 '16 at 01:12
  • @HenryDev So how can I get it done? I am completely lost on how to get it to work for both. i can get it to work only for on click or only for keypress but I Need it to work for both on click of calculate and when Enter is pressed! – Gurnoor Kahlon Nov 14 '16 at 01:22
  • @HenryDev any update? – Gurnoor Kahlon Nov 14 '16 at 01:47
  • go create a working snippet or fiddle dude – Rani Moreles Rubillos Nov 14 '16 at 01:50
  • @RaniMorelesRubillos https://jsfiddle.net/e2nf73bu/ – Gurnoor Kahlon Nov 14 '16 at 01:52
  • @GurnoorKahlon have you seen my answer mate ? – Rani Moreles Rubillos Nov 14 '16 at 02:30
  • Possible duplicate of [Trigger a button click with JavaScript on the Enter key in a text box](http://stackoverflow.com/questions/155188/trigger-a-button-click-with-javascript-on-the-enter-key-in-a-text-box) – Gurnoor Kahlon Nov 14 '16 at 03:25
  • *Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve].* You are not supposed to destroy your post once you get your question answered and you shouldn't post text as an image. – BSMP Nov 14 '16 at 04:45
  • Please do not vandalise your questions here, I have rolled it back to the last good version. – halfer Nov 14 '16 at 10:03

2 Answers2

0

Add this code to your page:

<script>
$(function(){
    $('input#commonR').keypress(function(event) {
        var keycode = event.keyCode || event.which;
        if(keycode == '13') {
            $('#submit').click();    
        }
    });
}); //END document.ready
</script>
cssyphus
  • 37,875
  • 18
  • 96
  • 111
  • Where exactly? I did something similar it didn't work! – Gurnoor Kahlon Nov 14 '16 at 02:18
  • See update - insert that at the bottom of your document, just above the `

    ` closing tag. Note the document.ready wrapper `$(function() {} );` -- it ensures the document if fully loaded before binding the javascript to the input element. Without that, the js would fire before the element was rendered, causing the js to not work.

    – cssyphus Nov 14 '16 at 22:14
0

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()
  }
})