Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to advance to the next form input when the current input has a value?

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 up to the last field. My question is, is it possible to simulate what happens when pressing the 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?

like image 589
praveenjayapal Avatar asked Nov 26 '09 12:11

praveenjayapal


People also ask

How do you activate the next input field on Enter?

Order entry form contains product code and quantity columns in multiple rows and delete button (-) in end of every row. It contains also add button in first column and after form. Pressing enter key should set focus to next text or numeric input field (product code or quantity), skipping buttons.

How can I get the value entered in the input field?

We can get the value of the text input field using various methods in JavaScript. There is a text value property that can set and return the value of the value attribute of a text field. Also, we can use the jquery val() method inside the script to get or set the value of the text input field.


2 Answers

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();        }     } }); 
like image 150
krcko Avatar answered Sep 24 '22 12:09

krcko


Needed to emulate the tab functionality a while ago, and now I've released it as a library that uses jquery.

EmulateTab: A jQuery plugin to emulate tabbing between elements on a page.

You can see how it works in the demo.

if (myTextHasBeenFilledWithText) {   // Tab to the next input after #my-text-input   $("#my-text-input").emulateTab(); } 
like image 40
Joel Purra Avatar answered Sep 25 '22 12:09

Joel Purra