MediaWiki:Common.js

Revision as of 23:43, 27 July 2025 by Garrett (talk | contribs) (Attempt to make forms generated by Page Forms more accessible.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/* Any JavaScript here will be loaded for all users on every page load. */

// === Page Forms label‑for‐input accessibility fix ===
( function () {
  $( function () {
    // Only run on a Page Forms form
    if ( !$( '#pfForm' ).length ) {
      return;
    }
    // For each field, hook up its visible label to its input/textarea
    $( '#pfForm .pf-field' ).each( function () {
      var $field = $( this );
      var $label = $field.find( '.pf-field-label, .oo-ui-labeledElement-label' ).first();
      if ( !$label.length ) {
        return;
      }
      var labelText = $label.text().trim();
      var $input = $field.find( 'input, textarea' ).first();
      if ( !$input.length || !labelText ) {
        return;
      }
      // Ensure an id on the input
      var id = $input.attr( 'id' );
      if ( !id ) {
        id = 'pf-' + labelText.replace( /\W+/g, '-' ).toLowerCase();
        $input.attr( 'id', id );
      }
      // Link the <label> and add aria‑label
      $label.attr( 'for', id );
      $input.attr( 'aria-label', labelText );
    } );
  } );
}() );