-4

I have a textbox in HTML:

<input id="myInfo">

I would like to run an AJAX function as soon as the user enters in some value into the textbox using jQuery. I want the function to be called as soon as the user presses the "Enter"

Can someone please provide a very simple illustration as to how this would be accomplished?

Thank you

minx
  • 33
  • 1
  • 6

2 Answers2

0

You could do something like

document.getElementById('myInfo').addEventListener('keypress', function(event) {
  // you could also do keyCode === 13
  if (event.key === 'Enter') {
    console.log('do ajax request here');
  }
})

Demo here: http://codepen.io/3stacks/pen/jyVpQP?editors=1111

3stacks
  • 1,880
  • 1
  • 16
  • 21
0
$('#myInfo').on('keydown', function(e){
    if(e.keyCode == 13)
    {
        //your code
    }
});
wuha
  • 15
  • 5