Set focus on button that triggered page reload

1k views Asked by At

I have a page with multiple buttons that can trigger a 'Add item to list'. This is a js function that after adding item to list will reload page so the total number of items is refreshed on shopping cart.

I need to set focus after reload on the button that triggered the refresh. (for accessibility)

How can I determine what triggered the refresh?

1

There are 1 answers

0
Brisbe On

My suggested method for this:

  1. As part of your JS procedure, get the id of the button in question. JavaScript - onClick to get the ID of the clicked button
  2. Store the id of the button in a hidden variable (focusButton, etc) that persists on the page between refreshes (through being declared in the View and Model in question, and not wiped through the Controller method in question).
  3. When loading the page, if that variable is set, set focus to the id listed in the variable, then set the variable to blank.