Nicholas
Nicholas

Reputation: 37

Submit when enter is pressed on number input

I want to make it so when I press enter when clicked on this number input box

<input id="answer" type="number" style="display: none;" placeholder="Answer Box"/>

it runs this function

NextQuestion()

I want it so when I press enter it runs the function

Upvotes: 0

Views: 139

Answers (3)

Mani
Mani

Reputation: 949

Use can use .keypress() and .click() function in Jquery to bind multiple events to same function

$('#answer').keypress(function(e){
    if(e.which == 13)
        //Enter key code is 13, this will capture when enter key pressed
        NextQuestion();
});

$('#answer').click(function(e){
    NextQuestion();
});

Upvotes: 1

Munawir
Munawir

Reputation: 3356

You can use keyup event listener

var doc = document.getElementById("answer");
doc.addEventListener("keyup", function(e){
  if(e.which==13) {
    alert('Enter key pressed, move to next question')
    //NextQuestion();
  }
});
<input id="answer" type="number" style="" placeholder="Answer Box" />

Upvotes: 0

Nico_
Nico_

Reputation: 1386

You have to bind on keyUp on this input and check if the keyCode is enter (13). In jQuery it will be :

$("input#answer").on("keyup",function(e){
    if(e.which==13)
       NextQuestion();
});

Upvotes: 1

Related Questions