.enterPressed() for jQuery

March 17, 2012 at 5:58 PMPhonicUK

A simple and reusable way to trap when enter is pressed on an element...

$.fn.enterPressed = function (callback) {
	this.keypress(function (event) {
		if (event.keyCode == '13') {
			event.preventDefault();
			callback();
		}
	});
};

$("#myElement").enterPressed(function(){alert($(this).val());});

Simple enough, isn't it?

Posted in: Web Development

Tags: , , ,