focusing on next input (jquery)

jquery focus next input on enter
focus on next input javascript
jquery focus next tabindex
jquery next
focus next input react
javascript move focus to next element
jquery focus on div
how to set focus on next textbox on enter key using javascript

I've got four inputs that each take one number. What I want to do is set the focus automatically to the next input once the number has been set. They all have the class "inputs".

This didn't quite work:

$(".inputs").keydown(function () {

            $(this).next().focus();
        });

I would suggest setting maxlength as 1 to each textbox and switch to next one once the val.length and the maxlength is same.

DEMO

$(".inputs").keyup(function () {
    if (this.value.length == this.maxLength) {
      $(this).next('.inputs').focus();
    }
});

Edit: Spent some time for the following (not fully tested, but basic tests worked fine)

   1. Allowing just numeric chars  
   2. Allow some control like del, backspace, e.t.c
   3. Backspace on empty textbox will move to prev textbox
   4. charLimit var to dynamically decide how many char you want to restrict.

Code:

$(function() {
    var charLimit = 1;
    $(".inputs").keydown(function(e) {

        var keys = [8, 9, /*16, 17, 18,*/ 19, 20, 27, 33, 34, 35, 36, 37, 38, 39, 40, 45, 46, 144, 145];

        if (e.which == 8 && this.value.length == 0) {
            $(this).prev('.inputs').focus();
        } else if ($.inArray(e.which, keys) >= 0) {
            return true;
        } else if (this.value.length >= charLimit) {
            $(this).next('.inputs').focus();
            return false;
        } else if (e.shiftKey || e.which <= 48 || e.which >= 58) {
            return false;
        }
    }).keyup (function () {
        if (this.value.length >= charLimit) {
            $(this).next('.inputs').focus();
            return false;
        }
    });
});

DEMO

jquery .next('input').focus(), You can use prevAll and nextAll with the selector :first to find the next and the previous element: $('.passcoder').keyup(function(e){ if (e.which  I have this jquery function that will focus on the next textbox while pressing 'enter' key, with square brackets in the name attribute. I am able to make it focus to the next input inside the same div e.g a[] to b[], b[] to c[], but i couldn't make it focus to the a[] in next div from c[].

That will just get the next element, whatever it is. You probably want:

$(".inputs").keyup(function () {
  $(this).next(".inputs").focus();
});

Also, key up not keydown or it will change too soon.

jQuery move focus to next input, I have a form that contains multiple inputs on different levels. Is there a jquery function that can simulate the pressing of 'TAB' key? I tried using. jQuery focus() Method: The focus event happens when an element gets focus (Either selected by a mouse click or by “tab-navigating” to it). This method triggers the focus event, or adds a function to run when the focus event occurs.

try this

jQuery.extend(jQuery.expr[':'], {
    focusable: function (el, index, selector) {
        return $(el).is('a, button, :input,[tabindex]');
    }
});
$(document).on('keypress', 'input,select', function (e) {
    if (e.which == 13) {
        e.preventDefault();
        // Get all focusable elements on the page
        var $canfocus = $(':focusable');
        var index = $canfocus.index(document.activeElement) + 1;
        if (index >= $canfocus.length) index = 0;
        $canfocus.eq(index).focus();
    }
});

Focus next element on keypress, It's been a year or two since I last dabbled in jQuery and have attempted to write a simple "on key press move to the next element" script. Read How to Set focus on First textbox of page using jQuery. There is a keydown function which is binded to all the textbox using bind metohd. There is a keydown function which is binded to all the textbox using bind metohd.

Here is the code I use for making enter key to behave as tab, i.e, focus to next element when pressing the Enter key or focusing previous element when pressing shift+Enter.

1) Essentially:

tabables = $("*[tabindex != '-1']:visible");
var index = tabables.index(element);
tabables.eq(index + 1).focus();

2) Here you are a "class" that encapsulates the behaviour, having in mind fordward and backwards and valid focusable elements.

I hope it helps and if some code suits your needs, feel free to adapt to your needs :)

EnterAsTab = function () {
    this.ENTER_KEY = 13;
};

EnterAsTab.prototype.init = function () {
    this.listenOnEnterKey();
};

EnterAsTab.prototype.listenOnEnterKey = function () {

    var me = this;
    $('form input').on('keypress', function (event) {

            if (event.which === me.ENTER_KEY) {

                if (!event.shiftKey)
                    me.findNextFocusableElement(this);
                else
                    me.findPreviousFocusableElement(this);

                event.preventDefault();
            }
        }
    );
};

EnterAsTab.prototype.findNextFocusableElement = function (element) {
    this.findFocusableElement(element, this.increaseIndex);
};

EnterAsTab.prototype.findPreviousFocusableElement = function (element) {
    this.findFocusableElement(element, this.decreaseIndex);
};

EnterAsTab.prototype.findFocusableElement = function (element, callable) {

    var tabables = $("*[tabindex != '-1']:visible");
    var index = tabables.index(element);
    var counter = 1;
    var nextElement = undefined;

    try {

        while (true) {

            if ((nextElement = tabables.eq(index + counter)).length === 0) {
                break;
            }

            if (this.isFocusableElement(nextElement)) {

                var newIndex = callable.call(this, index, counter);
                tabables.eq(newIndex).focus();

                break;
            } else {
                counter++;
            }
        }
    } catch (error) {
        console.log(error);
    }

};

EnterAsTab.prototype.increaseIndex = function (index, counter) {
    return (index + counter);
};

EnterAsTab.prototype.decreaseIndex = function (index, counter) {
    return index - counter;
};

EnterAsTab.prototype.isFocusableElement = function (element) {

    return ['SELECT', 'TEXTAREA'].indexOf(element.prop('tagName')) > -1 ||
        element.is(':text') ||
        element.is(':checkbox') ||
        element.is(':radio');
};

var enterAsTab = new EnterAsTab();
enterAsTab.init();

jQuery, focus() to next input field, based on input maxlength attribute , jQuery, focus() to next input field, based on input maxlength attribute - jquery-​focus-maxlenth.js. Following are scenarios I m implementing using JQuery in HTML When a user double click on table cell it should a new input inside the Double Clicked Cell [Done] When a user KeyPress [Tab] and it s

After searching and developing I end up in a crossbrowser snippet which makes it possible to focus the next input field with same class depending on maxlength (tested with 1 character) also with the ability to focus back via backspace button:

Javascript (jquery):

var codeCharInput = 'input.code-char';
$(codeCharInput+':first').focus();
$(codeCharInput).keyup(function(e) {
  if ((e.which == 8 || e.which == 46)) {
    $(this).prev(codeCharInput).focus().val($(this).prev().val());
  } else {
    if (this.value.length == this.maxLength) {
      $(this).next(codeCharInput).focus();
    }
  }
});

HTML:

<input type="text" name="chars[]" maxlength="1" class="code-char" />
<input type="text" name="chars[]" maxlength="1" class="code-char" />
<input type="text" name="chars[]" maxlength="1" class="code-char" />
<input type="text" name="chars[]" maxlength="1" class="code-char" />

jQuery, The element get focused by the mouse click or by the tab-navigating button. Syntax: $(selector).focus(function). Here selector is the selected element. Parameter: It  The focus event occurs when an element gets focus (when selected by a mouse click or by "tab-navigating" to it). The focus() method triggers the focus event, or attaches a function to run when a focus event occurs. Tip: This method is often used together with the blur() method.

jQuery focus() Method, Attach a function to the focus event. The focus event occurs when the <input> field gets focus: $("input").focus(function(){ $("span").css("display", "inline"). If a user clicks on an input that already has a value it will override it, instead of going to the next input, it will also only focus on text inputs. I had a situation where I had a submit input next to the text inputs and if using backspace could accidentally redirect the page.

jQuery: set focus on the next input field - JSFiddle, var fields = $(this).parents('form:eq(0),body').find('button,input,textarea,select');. 6. var index = fields.index(this);. 7. if (index > -1 && (index + 1) < fields.length) {. The input elements are all siblings of each other, so you can use $.next() to select the next one that appears after the event target. There is no need to name each input individually. There is no need to name each input individually.

How to focus on next input box after pressing enter. jquery , How to focus on next input box after pressing enter. jquery. This is my HTML: <div class="add_address_section"> <div class="address_field"> <input type="text"  .focus( handler )Returns: jQuery. Description: Bind an event handler to the "focus" JavaScript event, or trigger that event on an element. Type: Function( Event eventObject ) A function to execute each time the event is triggered. An object containing data that will be passed to the event handler.

Comments
  • See this post [1]: stackoverflow.com/questions/1232379/…
  • See updated post for some new features.. more like autotab DEMO
  • Wow this is excellent. I'll combine that with click(), so it resets the value when the user selects an already filled textbox. Right now if the user selects a filled input and types a number it focuses on the not the next input but the one after it (assuming all textboxes are filled). In an ideal scenario I guess it would not require deleting the number before typing a new one (it would replace it automatically). I can see how this would become problematic to implement with more than 1 char limit though. Just my thoughts. click() will do the trick for me anyway :)
  • @domino Nice find, but just couldn't let go of that.. so here the fix >> jsfiddle.net/skram/qygB2/5 << . Please try and let me know.
  • if there are input type disabled what should i modify to make it working ?
  • Ah yes. Could the keyup function ignore the delete/backspace key somehow?
  • Actually there's a bunch of other keys such as the tab key etc that would cause problems. I guess the easiest way would be to filter numbers only?
  • @domino That would be a different question.