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.
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.
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.
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.
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.
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>
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:
display: none
),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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With