---
title: "How to add tabindex dynamically using jquery?"  
description: "How to add tabindex dynamically using jquery?"  
author: "Revati S Misra"  
published: 2023-01-22  
updated: 2023-06-12  
canonical: https://www.mindstick.com/forum/157339/how-to-add-tabindex-dynamically-using-jquery  
category: "jquery"  
tags: ["jquery", "javascript", "html"]  
reading_time: 3 minutes  

---

# How to add tabindex dynamically using jquery?

[Add](https://www.mindstick.com/forum/12983/add-address-in-textbox-when-page-is-load-and-if-i-search-address-in-textbox-show-in-map-by-javascript) tabindex in all [HTML input](https://www.mindstick.com/forum/157343/how-to-prevent-a-user-from-adding-same-character-over-and-over-in-html-input-box-using-javascript) [fields](https://www.mindstick.com/forum/159126/sql-query-for-all-tables-and-fields-in-an-oracle-db) dynamically using jquery.

## Replies

### Reply by Aryan Kumar

To add tabindex dynamically using jQuery, you can use the following code:

Code snippet

```plaintext
$('element').attr('tabindex', value);
```

element is the selector for the element you want to add the tabindex to. value is the value of the tabindex attribute. For example, to add a tabindex of 0 to all input elements, you would use the following code:

Code snippet

```plaintext
$('input').attr('tabindex', 0);
```

You can also use the jQuery each() method to add tabindex to multiple elements at once. The following code will add a tabindex of 0 to all input elements that are currently visible:

Code snippet

```plaintext
$('input:visible').each(function() {
  $(this).attr('tabindex', 0);
});
```

The tabindex attribute is used to control whether or not an element is focusable. An element with a tabindex of 0 is focusable, meaning that it can be selected by pressing the Tab key. An element with a tabindex of -1 is not focusable, meaning that it cannot be selected by pressing the Tab key.

It is important to note that not all elements can have a tabindex attribute. For example, elements that are not part of the document's natural tab order, such as images and iframes, cannot have a tabindex attribute.

Here are some additional tips for using the tabindex attribute:

- Use a tabindex of 0 for elements that you want users to be able to focus on and interact with.
- Use a tabindex of -1 for elements that you do not want users to be able to focus on.
- Use a positive tabindex value for elements that you want to appear in a specific order in the tab order.
- Use the jQuery each() method to add tabindex to multiple elements at once.

### Reply by Sai Buildrers & Aatma Pvt. Ltd.

The tabindex is specifies the order of [HTML](https://www.mindstick.com/articles/1530/design-a-simple-stylish-calculator-using-html-css-and-javascript) [input](https://www.mindstick.com/forum/159209/how-can-i-read-convert-an-input-stream-into-a-string-in-java) elements. It is used to navigate the HTML element when the tab button is pressed. The tabindex is used in HTML elements as its attribute or property. It is used in HTML elements as an attribute to manage keyboard focus.

You can set the value in attribute tabindex as 1 and -1. When set tabindex = “0”, “1”, ”2”, ”3” etc. then focuses that HTML element when press tab button from keyboard and then set tabindex = “-1” then it will not focus that HTML element because it removes the element from the default navigation. If you set “0” in all HTML elements then it specifies the default order.

There can two ways to set the tabindex attribute in HTML elements,

1- Set as an attribute of HTML input element-

```plaintext
<div tabindex='-1'></div>
<div tabindex='0'></div>
<div tabindex='1'></div>
<div tabindex='2'></div>
<div tabindex='3'></div>
```

2- Set tabindex attribute in html form dynamically-

- Set in all visible fields in form

```plaintext
$('form:input:visible').each(function() { 
   $(this).attr('tabindex', '0');// value can set 0,1,2,3.
 });
```

- Ignore only all hidden fields in form

```plaintext
$('form:input:not(:hidden)').each(function (i) {
 	$(this).attr('tabindex', i + 1);
 });
```


---

Original Source: https://www.mindstick.com/forum/157339/how-to-add-tabindex-dynamically-using-jquery

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
