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}); ...

Get jQuery and JavaScript Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.