views:

161

answers:

1

Is there simple JavaScript or jQuery script to navigate around form fields, similar to what phpMyAdmin does with dynamic fields. When you click CTRL+LEFT or CTRL+DOWN it "tabs" to the next field.

This is extending from the default TAB (go to next) and SHIFT+TAB (go to previous). I want the navigation to be a bit more extensive by adding up, down, left right, end, home and by rows/columns.

+2  A: 

I believe that it would look something like this:

$('input').keypress(function(){
    //if correct key combination
        $(this).next('input').focus();
});

Ah, from your comment: "because tab goes to next, shift tab goes to previous. I want to navigate around form fields by up, down, left right, end, beginning by rows/columns."

If you want a 2d grid of inputs you might need to use the ids:

<input id="input_1_1" />

(id = input_x_y)

The javascript would then be something like:

$('input').keypress(function(){
    var id = $(this).attr('id');
    id.split("_");
    // if up:
        $('#input_'+id[0]+'_'+(id[1] - 1)).focus();
    // if down:
        $('#input_'+id[0]+'_'+(id[1] + 1)).focus();
    // if left:
        $('#input_'+(id[0] - 1)+'_'+id[1]).focus();
    // if right:
        $('#input_'+(id[0] + 1)+'_'+id[1]).focus();
});
SeanJA
Obviously you would need to know the order when you are laying out the html
SeanJA