Skip to content
FrontDepth, Home

Back to the topic

Practice: ARIA, accessible names and focus management

Answer first, then compare. Rate yourself honestly — it drives your weak-areas list.

3 predict-output · 3 open

Questions up to level

Shows questions up to the selected level.

No practice questions for this level yet.

  1. Predict the outputJunior

    The code calls focus() on five different elements and logs which element is focused after each call. What does it log?

    JavaScript
    document.body.innerHTML = `
      <button id="save">Save</button>
      <div id="card">Card</div>
      <div id="panel" tabindex="-1">Panel</div>
      <button id="ghost" hidden>Ghost</button>
      <button id="masked" aria-hidden="true">Masked</button>
    `;
    
    function tryFocus(id) {
      document.getElementById(id).focus();
      console.log(id + " -> " + document.activeElement.id);
    }
    
    tryFocus("save");
    tryFocus("card");
    tryFocus("panel");
    tryFocus("ghost");
    tryFocus("masked");
  2. Open questionJunior

    Why is <button> better than <div role="button">, and what is the first rule of ARIA?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.

  3. Predict the outputMiddle

    A Delete button opens a native modal dialog with showModal(). What does the code log?

    JavaScript
    document.body.innerHTML = `
      <main id="page">
        <button id="delete">Delete</button>
      </main>
      <dialog id="confirm" aria-labelledby="title">
        <h2 id="title">Delete file?</h2>
        <button id="cancel">Cancel</button>
        <button id="ok">Delete</button>
      </dialog>
    `;
    
    const active = () => document.activeElement.id;
    const dialog = document.getElementById("confirm");
    
    document.getElementById("delete").focus();
    console.log(active());
    
    dialog.showModal();
    console.log(active());
    
    document.getElementById("delete").focus();
    console.log(active());
    console.log(document.getElementById("page").inert);
    
    dialog.close();
    console.log(active());
  4. Open questionMiddle

    How does the browser compute a control's accessible name, and which naming mistakes do you see most often?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.

  5. Predict the outputSenior

    A list removes items in two ways: a naive removal, and one that moves focus before the item disappears. What does the code log?

    JavaScript
    document.body.innerHTML = `
      <h2 id="heading" tabindex="-1">Groceries</h2>
      <ul id="list">
        <li><button>Milk</button></li>
        <li><button>Eggs</button></li>
        <li><button>Bread</button></li>
      </ul>
    `;
    
    const buttons = () => [...document.querySelectorAll("#list button")];
    const active = () =>
      document.activeElement === document.body ? "BODY" : document.activeElement.textContent;
    
    function removeNaive(button) {
      button.closest("li").remove();
    }
    
    function removeAndRefocus(button) {
      const items = buttons();
      const i = items.indexOf(button);
      const next = items[i + 1] ?? items[i - 1];
      button.closest("li").remove();
      (next ?? document.getElementById("heading")).focus();
    }
    
    buttons()[1].focus();
    console.log(active());
    removeNaive(buttons()[1]);
    console.log(active());
    
    buttons()[0].focus();
    removeAndRefocus(buttons()[0]);
    console.log(active());
    removeAndRefocus(buttons()[0]);
    console.log(active());
  6. Open questionSenior

    Walk through building an accessible modal confirmation dialog. What happens to focus, and what does native <dialog> handle for you?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.