Skip to content

Instantly share code, notes, and snippets.

@karlgroves
Created December 24, 2013 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karlgroves/8114409 to your computer and use it in GitHub Desktop.
Save karlgroves/8114409 to your computer and use it in GitHub Desktop.
Found this and thought it looked interesting. I saw it at http://test.cita.illinois.edu/aria/tabpanel/tabpanel2.php#lsc1 and in that page they cite "ajpiano on the jQuery forums." Untested but seems sane
// focusable is a small jQuery extension to add a :focusable selector. Credit to ajpiano on the jQuery forums.
//
$.extend($.expr[':'], {
focusable: function(element) {
var nodeName = element.nodeName.toLowerCase();
var tabIndex = $(element).attr('tabindex');
// the element and all of its ancestors must be visible
if (($(element)[(nodeName == 'area' ? 'parents' : 'closest')](':hidden').length) == true) {
return false;
}
// If tabindex is defined, its value must be greater than or equal to 0
if (!isNaN(tabIndex) && tabIndex < 0) {
return false;
}
// if the element is a standard form control, it must not be disabled
if (/input|select|textarea|button|object/.test(nodeName) == true) {
return !element.disabled;
}
// if the element is a link, href must be defined
if ((nodeName == 'a' || nodeName == 'area') == true) {
return (element.href.length > 0);
}
// this is some other page element that is not normally focusable.
return false;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment