Live demo
$('.num').keydown(function (e) { if (e.which == 34) { $(this).closest('form').next('form').find('.num').focus(); return false; } if (e.which == 33) { $(this).closest('form').prev('form').find('.num').focus(); return false; } });
As you can see, I used .closest('form') , this will allow you to use textarea once inside the fieldset without worrying about jQuery, because it will always cross the DOM in the right way.
Another nice way: DEMO
$('.num').keydown(function (e) { var k = e.which; if ( k==34 || k==33 ){ $(this).closest('form')[k==34?'next':'prev']('form').find('.num').focus(); e.preventDefault(); } });
Another nice demo
var $num = $('.num').keydown(function(e) { var k=e.which, c=$num.index(this); if(k==34||k==33) $num[k==34?++c:--c].focus(); });
Skip the last one:
var $num = $('.num') // Create a jQuery Array collection // of all the .num in the DOM. .keydown(function(e) { // On keydown pass the (e)vent. var k=e.which, // Var k is now the "which keyCode" c=$num.index(this); // Var c will be the index of the clicked // element taken from the collection. if(k==34||k==33) // Only IF pgUp/pgDn $num[k==34?++c:
source share