Advertisement
Guest User

ARIA application role test (Javascript)

a guest
Jul 7th, 2011
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const DOM_VK_LEFT = 37;
  2. const DOM_VK_RIGHT = 39;
  3. var fruitItemsCount = 0;
  4.  
  5. function onBodyLoad() {
  6.     var fruitsElem = document.getElementById("fruits");
  7.     fruitsElem.addEventListener("focus", function(e) { onFruitsFocus(e); }, false);
  8.     fruitsElem.addEventListener("keydown", function(e) { onFruitsKeyDown(e); }, false);
  9.     fruitItemsCount = fruitsElem.getElementsByTagName("li").length;
  10. }
  11.  
  12. function onFruitsFocus(event) {
  13.     var elem = event.target;
  14.     var activeId = elem.getAttribute("aria-activedescendant");
  15.     var active = document.getElementById(activeId);
  16.     active.focus();
  17. }
  18.  
  19. function onFruitsKeyDown(event) {
  20.     var elem = event.currentTarget;
  21.     var active = elem.getAttribute("aria-activedescendant");
  22.     var fruitNum = active;
  23.     event.preventDefault();
  24.     if (event.keyCode === DOM_VK_RIGHT) {
  25.         ++fruitNum;
  26.         if (fruitNum > fruitItemsCount) {
  27.             fruitNum = 1;
  28.         }
  29.     } else if (event.keyCode === DOM_VK_LEFT) {
  30.         --fruitNum;
  31.         if (fruitNum < 1) {
  32.             fruitNum = fruitItemsCount;
  33.         }
  34.     }
  35.     if (fruitNum != active) {
  36.         var currItem = document.getElementById(fruitNum);
  37.         document.getElementById(active).className = "item";
  38.         currItem.className = "selectedItem";
  39.         elem.setAttribute("aria-activedescendant", fruitNum);
  40.         currItem.focus();
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement