0

This code is not triggered immediately:

$('form').on('change', '#price', function(){
   // code here
});

but only when I click from the input box somewhere else.

How to trigger the code even when the user is typing?

Now, it is waiting till he click away from the input box.

4 Answers4

2

Use input instead:

$('form').on('input', '#price', function(){
   // code here
});

input event

The DOM input event is fired synchronously when the value of an <input> or <textarea> element is changed. Additionally, it's also fired on contenteditable editors when its contents are changed. In this case, the event target is the editing host element. If there are two or more elements which have contenteditable as true, "editing host" is the nearest ancestor element whose parent isn't editable. Similarly, it's also fired on root element of designMode editors.


You can use keyup and keydown events but note that if you use keyup or keydown when you paste something in your text box/textarea the change will not be fired.

Read this thread to see the differences between the two events.

JSFIDDLE

Community
  • 1
  • 1
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
0

Use keyup event to do something when user is typing:

$('#price').on('keyup', function() {
     /* do something */
});
Nikolai Samteladze
  • 7,699
  • 6
  • 44
  • 70
0

Take a look to this: http://api.jquery.com/keyup/

and a little example reported in that page.

 $( "#target" ).keyup(function() {
   alert( "Handler for .keyup() called." );
 });

So when you press a key and the release it, that event is triggered

Kevin Cittadini
  • 1,449
  • 1
  • 21
  • 30
0

I presume #price is your input. ??

To trigger the code when typing you can use this:

$('form').on('keyup', '#price', function(){
    // code here
});

you could either use keyup or keydown events

http://api.jquery.com/keyup/

Jonathan Marzullo
  • 6,879
  • 2
  • 40
  • 46