I'm trying to write a simple plugin, enterkeypress, which extends the keypress event, but only fires when the key pressed is the enter key:
(function ($) {
$.fn.enterkeypress = function (fn) {
return this.keypress(function(event){
if(event.which == '13'){
event.preventDefault();
fn();
}
});
};
})(jQuery);
when testing this out, it correctly fires the event but the 'this' keyword references the window, not the element.
$(":text").enterkeypress(function(){console.log(this)});
that code will output Window in the console... I'd rather it output the input control.
What am I doing wrong here?