Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Untitled

a guest Nov 12th, 2018 86 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. // Add a ":focusable" pseudo-class
  2. jQuery.expr[':'].focusable = function (elem, i, argument) {
  3.     return jQuery(elem).is('a, button, :input, [tabindex]');
  4. };
  5.  
  6. jQuery(document).ready(function () {
  7.  
  8.     // To improve accessibility, trap focus within a Bootstrap modal dialog
  9.     // when it's open, and release it when the modal closes.
  10.     // Add a handler to the Boostrap 3 "show" event. When the dialog opens,
  11.     // the focus is set to the dialog's first focusable element. This should
  12.     // typically be the close button in the upper right corner.
  13.     // To trap focus within the dialog, we also attach a handler to the
  14.     // "focusin" event at document level. When focus moves to any control
  15.     // outside the dialog, we move it back to the focus point.
  16.     // A handler on the Bootstrap "hide" event removes the "focusin" handler
  17.     // when the dialog closes.
  18.     //
  19.     // We attach the handler to the body element with a .modal selector to
  20.     // handle dynamically created dialogs (via Knockout or similar).
  21.     jQuery("body").on("show.bs.modal", ".modal", function (e) {
  22.  
  23.         // Save reference to the dialog element
  24.         var dialog = e.target;
  25.  
  26.         // Get the first focusable element in the dialog, and focus there.
  27.         var focuspoint = $(":focusable", dialog).get(0);
  28.         focuspoint.focus();
  29.  
  30.         // If focus moves to a control outside the dialog, move it back.
  31.         jQuery(document).on("bootstrap-accessibility.focusin", function (e) {
  32.             if (!(jQuery.contains(dialog, e.target))) {
  33.                 // Move focus back to dialog. That will cause this handler to fire again, but that's OK.
  34.                 focuspoint.focus();
  35.             }
  36.         });
  37.     });
  38.  
  39.     // Stop focus trapping when dialog is closed
  40.     jQuery("body").on("hide.bs.modal", ".modal", function (e) {
  41.         jQuery(document).off("bootstrap-accessibility.focusin");
  42.     });
  43. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top