One of my most popular articles at my personal site, is an article I wrote a few years back on how to capture the “Enter” key using JavaScript. I was really into the script.aculo.us and Prototype JavaScript libraries back in those days. Since then, I have become quite a fan of jQuery. So I thought I would post an update on how to do the same with jQuery.
Make sure you include the jQuery library:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
The HTML:
<input type="text" id="text_field_1" value="" />
The JavaScript:
$('#text_field_1').keypress(function(e) { if (e.which == 13) { alert('The enter key was pressed!'); } });
Yes, it really is that simple. Try it out and let me know if I missed something.