This is an unpublished editor’s draft that might include content that is not finalized. View the published version

Skip to content

Technique SCR27:Reordering page sections using the Document Object Model

About this Technique

This technique relates to 2.4.3: Focus Order (Sufficient when used for changing a web page dynamically).

This technique applies to HTML, script.

Description

The objective of this technique is to provide a mechanism for re-ordering component which is both highly usable and accessible. The two most common mechanisms for reordering are to send users to a set-up page where they can number components, or to allow them to drag and drop components to the desired location. The drag and drop method is much more usable, as it allows the user to arrange the items in place, one at a time, and get a feeling for the results. Unfortunately, drag and drop relies on the use of a mouse. This technique allows users to interact with a menu on the components to reorder them in place in a device independent way. It can be used in place of, or in conjunction with drag and drop reordering functionality.

The menu is a list of links using the device-independent onclick event to trigger scripts which re-order the content. The content is re-ordered in the Document Object Model (DOM), not just visually, so that it is in the correct order for all devices.

Examples

Example 1

This example does up and down reordering. This approach can also be used for two-dimensional reordering by adding left and right options.

The components in this example are list items in an unordered list. Unordered lists are a very good semantic model for sets of similar items. The menu approach can also be used for other types of groupings.

In this example, the menu is always visible. This is a good approach for components that are not too numerous, as it allows users to see the options without having to open a menu.


  const list = document.getElementById('myList');

  // Attach a single event listener to the whole list
  list.addEventListener('click', function (e) {
    
    // Ensure the click came from a button
    if (e.target.tagName !== 'BUTTON') return;

    // Get the <li> that contains the clicked button
    const li = e.target.closest('li');

    // Move up: insert the clicked <li> before its previous sibling
    if (e.target.classList.contains('up') && li.previousElementSibling) {
      li.parentNode.insertBefore(li, li.previousElementSibling);
    }

    // Move down: insert the next sibling before the clicked <li>
    if (e.target.classList.contains('down') && li.nextElementSibling) {
      li.parentNode.insertBefore(li.nextElementSibling, li);
    }
  });
Working example of this code: Reorder list with buttons

Tests

Procedure

  1. Find all components which can be reordered via drag and drop.
  2. Check that there is also a mechanism to reorder them using menus containing buttons, or other appropriate controls.
  3. Check that the menus are contained within the re-orderable items in the DOM.
  4. Check that scripts for reordering are triggered only from the onclick event of the menu controls.
  5. Check that items are reordered in the DOM, not only visually.

Expected Results

  • #2 through #5 are true.
Back to Top