Return to Snippet

Revision: 12020
at March 12, 2009 09:29 by kouphax


Updated Code
$('input').live("keypress", function(e) {
                /* ENTER PRESSED*/
                if (e.keyCode == 13) {
                    /* FOCUS ELEMENT */
                    var inputs = $(this).parents("form").eq(0).find(":input");
                    var idx = inputs.index(this);

                    if (idx == inputs.length - 1) {
                        inputs[0].select()
                    } else {
                        inputs[idx + 1].focus(); //  handles submit buttons
                        inputs[idx + 1].select();
                    }
                    return false;
                }
            });

Revision: 12019
at March 12, 2009 06:05 by kouphax


Updated Code
$('input').keypress(function(e){
  /* ENTER PRESSED*/
  if(e.keyCode == 13){
    /* ONE LINER TO FOCUS ELEMENT*/
    try{
      ($(this).next(":input")[0] || $(this).parent("form").find("input:first")[0]).focus();
    }catch(e){
      /* IGNORE */
    }
  }
});

Revision: 12018
at February 27, 2009 08:54 by kouphax


Initial Code
$('input').keypress(function(e){
  /* ENTER PRESSED*/
  if(e.keyCode == 13){
    /* ONE LINER TO FOCUS ELEMENT*/
    try{
      ($(this).next(":input")[0] || $(this).parent("form").find("input:first")[0]).focus();
    }catch(e){
      /* IGNORE */
    }
});

Initial URL

                                

Initial Description
Simple function for turning RETURN key presses into TAB key presses.  Focuses input element next in the DOM (usually makes sense), unless we are at the end of the form, at which point it focuses the first element in the form for cyclical purposes.

Initial Title
jQuery Tab On Return Key Press

Initial Tags
javascript, jquery

Initial Language
JavaScript