1

I am trying to test the tabbing on a page but I cannot SEE what I've just tabbed to.

I managed to highlight the input element types text and textarea pretty easily. Radio did not work, but affecting the parent element did work. Here's my jquery:

$(":input").focusin( function() {
    if($(this).attr('type') === 'radio')  {
        $(this).parent().attr('style', 'background-color: yellow !important');
    } else {
        $(this).attr('style', 'background-color: yellow !important');
    }
});
$(":input").focusout( function() {
    if($(this).attr('type') === 'radio')  {
        $(this).parent().css('background-color','');
    } else {
        $(this).css('background-color','');
    }
});

But now I still have some other elements that I'm tabbing to and I cannot even tell what they are. All I know is it takes several tab presses to get from the last radio group to the next "select" element (which I can see is focused even without extra jquery code). After that is a button, which I also would like to be highlighted.

Do I have to list conditions for every single element? Or use a selector like $(":input,:button,[etc]")? Is there some easy way I can just have the element, whatever it is, highlighted after it is tabbed to?

Buttle Butkus
  • 9,206
  • 13
  • 79
  • 120

2 Answers2

3

You can use the :focus pseudo-class in CSS. It is well supported except IE 7 and older; if you need to support them, use a polyfill like Selectivzr. However, note that :focus matches the focused element, no matter how it reached focus (e.g. via tabbing, via a click, or an invocation of the focus() method).

Example:

<style>
  :focus { outline: solid red } 
</style>
<p><input placeholder="Click me, then tab">
<p><input type=radio>
<p><a href="https://developer.mozilla.org">link</a>
<p contenteditable=true>This is editable and focusable
Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390
0

You might want to use tabindex attribute for HTML tags. Here is a SO question: What is the HTML TabIndex

Community
  • 1
  • 1
Erdem E.
  • 170
  • 1
  • 5