/ Published in: JavaScript
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.
Expand |
Embed | Plain Text
$('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; } });
Comments
Subscribe to comments
You need to login to post a comment.

$('input').keypress(function(e){ /* ENTER PRESSED/ if(e.keyCode == 13){ / ONE LINER TO FOCUS ELEMENT/ try{ ($(this).blur(); }catch(e){ / IGNORE */ } } });
@kcmr - 'fraid not - $(this).blur() doesn't set focus on the next element merely blurs the current element.