views:

550

answers:

2

Hi Friends, I am having a form with lots of entries. I would like to change my focus to the next textbox, once i entered the value in the current textbox. and want to continue this process upto the last field. My question is, is it possible to simulate tab key through javascript coding once i enter the value in the text box.

Without pressing the tab key in keyboard, i would like to bring the same functionality through javascript. Is this possible ?

+1  A: 
function nextField(current){
    for (i = 0; i < current.form.elements.length; i++)
     if (current.form.elements[i].tabIndex == current.tabIndex+1) {
      current.form.elements[i].focus();
      if (current.form.elements[i].type == "text")
       current.form.elements[i].select();
      }
     }
    }
}

This, when supplied with the current field, will jump focus to the field with the next tab index. Usage would be as follows

<input type="text" onEvent="nextField(this);" />
Gausie
+1  A: 

you just need to give focus to the next input field (by invoking focus()method on that input element), for example if you're using jQuery this code will simulate the tab key when enter is pressed:

var inputs = $(':input').keypress(function(e){ 
    if (e.which == 13) {
       e.preventDefault();
       var nextInput = inputs.get(inputs.index(this) + 1);
       if (nextInput) {
          nextInput.focus();
       }
    }
});
krcko