set tabindex=0 with query selector in javascript

To set tabindex to 0 using query selector in JavaScript, we can use the following code snippet:

index.tsx
document.querySelector("#elementId").setAttribute("tabindex", "0");
68 chars
2 lines

In the above code, we are using setAttribute method of the selected element to set the tabindex to 0.

Here, #elementId is the ID of the HTML element which we want to set the tabindex attribute.

Alternatively, we can also use tabIndex property of the element to set the tabindex as shown below:

index.tsx
document.querySelector("#elementId").tabIndex = "0";
53 chars
2 lines

This will achieve the same result as setAttribute method.

gistlibby LogSnag