Move Focus and Announce Changes

In Make Controls Work for Everyone, you tested Sam’s shopping list with the keyboard alone, and the Show only today button learned to say whether it’s pressed. Every control can now be reached and used. What happens after a control does its job is another matter. Add an item with Enter, and the cursor stays in How many?, so Sam has to work back to Item before typing the next one. A screen reader user hears nothing about the new row at all.

In this lesson, you’ll predict where the focus is after the page changes, and you’ll write handlers that put it somewhere sensible. You’ll also make the page say what changed and tie an error message to the box that needs fixing. Start by removing an item, the change that loses the focus most completely.

Put the Focus Where the Next Step Is

Sam crosses items off a paper list once they’re in the basket, so the page needs a Remove button on each row. Priya’s guest list had the same buttons, drawn by render() and handled by one delegated listener on the list. Sam’s list works the same way.

In render(), add these lines before the if statement that checks todayOnly:

Language: JavaScript Filename: kitchen/main.js
    const remove = document.createElement("button");
    remove.className = "remove";
    remove.dataset.itemId = item.id;
    remove.textContent = `Remove ${item.name}`;
    row.append(" ", remove);

The remove class lets the listener find the button, and dataset.itemId writes the record’s ID into a data-item-id attribute. The append() call adds a space and then the button to the row. Each button’s text names its item, so Remove eggs and Remove fresh basil say which row they act on, even when a screen reader reads a button on its own.

Now add the following function and listener at the end of main.js. The function finds a record’s position by its ID:

Language: JavaScript Filename: kitchen/main.js
function indexOfItem(id) {
  for (let i = 0; i < items.length; i++) {
    if (items[i].id === id) {
      return i;
    }
  }
  return -1;
}

The listener asks closest() for the Remove button the click came from, and it ignores clicks anywhere else in the list. The call to splice() removes one record from the array, and checkName() runs after render(), as you did in Add Your Own Validation Rule, so a removed item can be added again. The last line is a temporary log:

Language: JavaScript Filename: kitchen/main.js
itemList.addEventListener("click", (event) => {
  const button = event.target.closest(".remove");
  if (button === null) {
    return;
  }
  items.splice(indexOfItem(button.dataset.itemId), 1);
  render();
  checkName();
  console.log("focus:", document.activeElement.tagName);
});

The document.activeElement property refers to the element that has the focus right now, and tagName is its tag’s name in capitals. Save, then add eggs (3), basmati rice (1), and fresh basil (2), pressing Enter in How many? each time:

The focus ring is still in How many?, where you pressed Enter. Now press Tab until the ring reaches Remove basmati rice, press Enter, and look at the Console:

Language: Program Output
focus: BODY

The rice row is gone, and so is the focus. The button that had it was removed, and render() redrew every row, so even Remove eggs is a new element now. The browser can’t keep focus on an element that isn’t on the page, so it falls back to the <body>. Press Tab, and the ring lands on Remove eggs, back at the top of the list. A keyboard user has lost their place, and a screen reader user isn’t told where they are.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.