Question

I write the following code all the time to handle when the enter key pressed:

$("#selectorid").keypress(function (e) {
    if (e.keyCode == 13) {
        var targetType = e.originalTarget
            ? e.originalTarget.type.toLowerCase()
            : e.srcElement.tagName.toLowerCase();
        if (targetType != "textarea") {
            e.preventDefault();
            e.stopPropagation();
            // code to handler enter key pressed
        }
    }
});

Is there a way to extend jQuery so that I could just write:

$("#selectorid").enterKeyPress(fn);
Was it helpful?

Solution

You can extend jquery something like:

jQuery.fn.returnPress = function(x) {
  return this.each(function() {
    jQuery(this).keypress(function(e) {
      if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
        x();
        return false;
      }
      else {
        return true;
      }
    });
  });
};

Which can be invoked like:

$('selector').returnPress(function() { alert('enter pressed'); });

OTHER TIPS

You can do what David G says, but perhaps the most correct way to approach this would be to write a custom event:

$(document).keypress(function(evt){
    if(evt.keyCode==13) $(evt.target).trigger('enterPress');
});

Which could be bound like so:

$(document).bind('enterPress', fn);

See an example here: http://jquery.nodnod.net/cases/1821/run

The advantage to this approach is that you can bind, unbind, namespace, and trigger the event like any other event in jQuery.

You can define it as a plugin with a bit less code like this:

jQuery.fn.enterKeyPress = function(callback) {
  return this.not("textarea").keypress(function (e) {
    if (e.keyCode == 13) {
      callback($(this));
      return false;
    }
  });
};

Use like this:

$("input").enterKeyPress(function() { alert('hi'); });

This approach still ignores <textarea>, but instead of checking every keystroke, we just never bind the keypress event to any textarea.

This is what I use to capture the enter key on any form element, and convert it into a tab. I have made it so the enter key works normally in a textarea, and on submit, reset and button elements.

$.fn.focusNext = function(e) {
  var t = $(this);
  if ( t.is(":submit")==true || t.is(":reset")==true || t.is("textarea")==true || t.is("button")==true ) { exit(); }

  if (e.which==13 || e.which==3) {
    return this.each(function() {
      e.preventDefault();
      var fields = $(this).parents("form:eq(0)").find(":input:visible");
      var index = fields.index( this );
      if ( index > -1 && ( index + 1 ) < fields.length ) { fields.eq( index + 1 ).focus(); }
    });
  }
  return true;
};

And to use it, it's called like so

$(":input").keypress(function(e) { $(this).focusNext(e); });

OR

$(":input").live("keypress", function(e) { $(this).focusNext(e); });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top