11

Just imagine a formular where there is a checkbox and a textfield. If someone starts typing into the textfield "bla bla bla whatever", the checkbox should tick itself. Is there a event that corresponds to the typing or do I have to use .focus ?

3 Answers 3

19

events which are fired on typing are:

  • onkeydown (jQuery: keydown)
  • onkeyup (jQuery: keyup)
  • onkeypress (jQuery: keypress)

You may create an event handler to any of those to modify or influence users input. Also be aware of .preventDefault() and .stopPropagation() functions, which prevent the default behavior for an element or suppress the event bubbling up the DOM.

References: keyup, keydown, keypress, preventDefault(), stopPropagation()

6

Yes, keyup() event handler. See http://api.jquery.com/keyup/

3

If you use jQueryUI the autocomplete function has a source event which is fired on typing, ctrl+v pasting and so on. It also has the huge advantage of specifying a minimum length and keystroke delay.

jQueryUI .autocomplete() is quite versatile and not just reserved for complex ajax tasks.

$("input#vin").autocomplete({
  delay: 500,
  minLength: 17,
  source: function () {
    // Do your stuff here, keep in mind that you can't use $(this) inside this closure, as it is closed in of the autocomplete function. 
    decodeVinAjax(autofillVehicleDetails, $('input#vin').val());
  }
});
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.