O'Reilly logo

jQuery and JavaScript Phrasebook by Brad Dayley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Detecting Changes to Text Input Elements

$("input[type=text]").on("keypress",   function(){     $("#p1").html("Key Pressed"); }); $("input[type=text]").on("keydown",   function(){     $("#p1").html("Key is Down"); }); $("input[type=text]").on("keyup",   function(){     $("#p1").html("Key is Up"); });

JavaScript provides three events that are triggered when a key is pressed on the keyboard: keydown, keypress, and keyup. These events allow you to capture each keystroke into the text input.

For example, if you want to add an event handler that is triggered each time a key is pressed in text input, you could use the following:

$("input[type=text]").on("keypress",   function(){handler_code}); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required