Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tabindex -1 not working for child elements

I have a div tag with some content getting loaded inside it. The content inside can have buttons, anchor elements, etc. which are focusable. I do not have control over the content but I can modify the 'div' tag attributes.

My problem is the focus still goes to the content (anchor, buttons, etc.) even if I specify the tabIndex -1 to the div tag.

<!-- HTML content here -->  <div tabindex="-1" id="externalContent">    <div>      ...      <button>click me</button> <!-- Focus shouldn't come here -->    </div>  </div>  <!-- HTML content here -->

Is there a way to skip the entire content while tabbing ? It's certainly not working with the above code.

like image 633
Adarsh Konchady Avatar asked Mar 13 '17 11:03

Adarsh Konchady


People also ask

What is the difference between Tabindex and Tabindex 0?

tabindex="1" (or any number greater than 1) defines an explicit tab or keyboard navigation order. This must always be avoided. tabindex="0" allows elements besides links and form elements to receive keyboard focus.

How do I select a specific child in Javascript?

Select the parent element whose child element is going to be selected. Use . querySelector() method on parent. Use the className of the child to select that particular child.

How do I get the div element of all children?

If You want to get list only children elements with id or class, avoiding elements without id/class, You can use document. getElementById('container'). querySelectorAll('[id],[class]'); ... querySelectorAll('[id],[class]') will "grab" only elements with id and/or class.

Can you add Tabindex to div?

DIV elements are not compatible with tabindex in HTML4). The following elements support the tabindex attribute: A, AREA, BUTTON, INPUT, OBJECT, SELECT, and TEXTAREA. Essentially anything you would expect to be able to hold focus; form elements, links, etc.


2 Answers

Not sure why nobody has mentioned visibility: hidden yet. Setting display: none can in some cases mess up logic when dealing with dimensions of non-visual elements. visibility will persist the dimensions just like opacity: 0 would do, but also disable any tabbable children.

Example:

<div style="visibility: hidden;">     <a href="#">I'm only tabbable if my parent is visible!</a> </div> 
like image 102
Nordling Art Avatar answered Sep 20 '22 15:09

Nordling Art


Setting tabindex="-1" allows you to set an element’s focus with script, but does not put it in the tab order of the page. It also does not pull the children of something out of keyboard tab order.

tabindex="-1" is handy when you need to move focus to something you have updated via script or outside of user action.

If you are trying to remove an element from tabindex altogether, whether for screen readers or keyboard users, you will likely have to choose between one of these:

  1. Hide it altogether (via display: none),
  2. Use script on the element so that when it receives focus, the script shifts the focus somewhere else.

Without context (a working URL, a reason that you want to do this), this sounds very much like the opposite of accessibility. I encourage you not to mess with focus unless you have a very good reason.

like image 44
aardrian Avatar answered Sep 20 '22 15:09

aardrian