Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6556431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:56:39+00:00 2026-05-25T12:56:39+00:00

I have two input elements, the first is focused, and I want to focus

  • 0

I have two input elements, the first is focused, and I want to focus the second by simulating the TAB keypress/keydown event. (Note: I don’t want to use .next() or such.)

This is my code, inspired from this question:

$('input').first().focus();

var e = $.Event('keydown');

e.which = 9; // TAB
$(':focus').trigger(e);

Please see http://jsfiddle.net/3PcPH/

The code doesn’t work. What is wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:56:40+00:00Added an answer on May 25, 2026 at 12:56 pm

    There is no simple programmatic way to do this using Javascript… so here’s a brute force way.

    According to W3:

    Elements that may receive focus should be navigated by user agents
    according to the following rules:

    1. Those elements that support the tabindex attribute and assign a
      positive value to it are navigated first. Navigation proceeds from the
      element with the lowest tabindex value to the element with the highest
      value. Values need not be sequential nor must they begin with any
      particular value. Elements that have identical tabindex values should
      be navigated in the order they appear in the character stream.
    2. Those
      elements that do not support the tabindex attribute or support it and
      assign it a value of “0” are navigated next. These elements are
      navigated in the order they appear in the character stream.
    3. Elements
      that are disabled do not participate in the tabbing order.

    I accomplished this by storing the order of elements in the form that have tabIndex > 0 in their tabIndex order and then concatenating the rest of the elements in the order they appear within the document. The following code simulates a tab keypress when focused on a form input and the letter ‘z’ is pressed (but you can change this to whatever condition you require):

    $(':input').keypress(function(e){ 
    
        // if 'z' pressed
        if (e.which == 122) {
    
            // if we haven't stored the tabbing order
            if (!this.form.tabOrder) {
    
                var els = this.form.elements,
                    ti = [],
                    rest = [];
    
                // store all focusable form elements with tabIndex > 0
                for (var i = 0, il = els.length; i < il; i++) {
                    if (els[i].tabIndex > 0 &&
                        !els[i].disabled && 
                        !els[i].hidden && 
                        !els[i].readOnly &&
                        els[i].type !== 'hidden') {
                        ti.push(els[i]);
                    }
                }
    
                // sort them by tabIndex order
                ti.sort(function(a,b){ return a.tabIndex - b.tabIndex; });
    
                // store the rest of the elements in order
                for (i = 0, il = els.length; i < il; i++) {
                    if (els[i].tabIndex == 0 &&
                        !els[i].disabled && 
                        !els[i].hidden && 
                        !els[i].readOnly &&
                        els[i].type !== 'hidden') {
                        rest.push(els[i]);
                    }
                }
    
                // store the full tabbing order
                this.form.tabOrder = ti.concat(rest);
            }
    
            // find the next element in the tabbing order and focus it
            // if the last element of the form then blur
            // (this can be changed to focus the next <form> if any)
            for (var j = 0, jl = this.form.tabOrder.length; j < jl; j++) {
                if (this === this.form.tabOrder[j]) {
                    if (j+1 < jl) {
                        $(this.form.tabOrder[j+1]).focus();
                    } else {
                        $(this).blur();
                    }
                }
            }
    
        }
    
    });
    

    See demo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two label elements. I want the first one to be shown all
I have two elements: <input a> <input b onclick=...> When b is clicked, I
I have two forms on one page and want to have the input boxes
Currently I have two pages: The first page contains an input form, and the
I have two <button> elements in one form. First <button> have a click handler,
So I have two text elements A and B, and on the onblur event
<input type=radio id=rad1 name=group value=one>First Option</input><br> <input type=radio id=rad2 name=group value=two>Second Option</input><br> <input type=radio
I'm trying to have two or more columns worth of DIVs for input elements
I have two input text boxes which are decimal. The sum of the two
I have two input fields, and without changing their names (i.e., I have to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.