css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
                      <div id="mainPage" aria-hidden="false">
            <h1>The Incredible Accessible Modal Window, Version 2</h1>
            <p><a href="#">Read the blog post about the updates to this demonstration (link coming soon).</a></p>
            <p><a href="https://github.com/gdkraus/accessible-modal-dialog">Get the code on GitHub</a></p>
            <p>This page demonstrates how to make a modal window as accessible as possible to assistive technology users. Modal windows are especially problematic for screen reader users. Often times the user is able to &quot;escape&quot; the window and interact with other parts of the page when they should not be able to. This is partially due to the way screen reader software interacts with the Web browser.</p>
            <h2>What's New In Version 2?</h2>
            <p>Version 2 builds on <a href="http://accessibility.oit.ncsu.edu/training/aria/modal-window/">Version 1</a> and incorporates some new features and handles improvements in screen reader support.</p>
            <ul>
                <li>A shim that had to be used for VoiceOver support is no longer necessary in OS X 10.9.</li>
                <li>There is a way to allow screen reader users to use their virtual cursor to read the contents of the modal dialog by including a role=&quot;document&quot; container inside the role=&quot;dialog&quot;. All screen readers could already read via the virtual cursor except for NVDA. One could argue this is a shim for NVDA, but it's not clear from the spec who is correct. A role=&quot;document&quot; is to be treated as an application, but other screen readers still let you browse via the virtual cursor. NVDA does not even let you enable the virtual cursor unless you add the role=&quot;document&quot; container.</li>
                <li>The description of the modal dialog is now referenced by aria-describedby.</li>
            </ul>
            <h2>The Accessible Modal Window in Action</h2>
            <p>To see this in action, you just need to <button id="startModal">view the modal window</button>. If the modal window works as planned, once the modal window is visible you should not be able to interact with other links on the main page like <a href="http://accessibility.ncsu.edu">going to our main accessibility page</a>. If you can interact with the page behind the modal window, guidance is given for how to get back to the modal window.</p>
            <h2>Features</h2>
            <p>This example implements the following features:</p>
            <ol>
                <li>The page is divided into three sections:
                    <ol>
                        <li>&lt;div id=&quot;mainPage&gt;&lt;/div&gt;</li>
                        <li>&lt;div id=&quot;modal&quot; role=&quot;dialog&quot;&gt;&lt;/div&gt;</li>
                        <li>&lt;div id=&quot;modalOverlay&quot;&gt;&lt;/div&gt;</li>
                    </ol>
                </li>
                <li>When the modal dialog is displayed, an overlay is placed over top of the mainPage so it is
                    <ol>
                        <li>visually grayed out in order to indicate you cannot interact with what is behind the window</li>
                        <li>not clickable with the mouse</li>
                    </ol>
                </li>
                <li>When the modal dialog is displayed, the mainPage is marked with aria-hidden=&quot;true&quot; to prevent screen readers from interacting with it once the modal dialog is open</li>
                <li>Keyboard access is limited to only interacting with the modal dialog once it is visible
                    <ol>
                        <li>The tab key loops through all of the keyboard focusable items within the modal window</li>
                        <li>This is determined programmatically through the DOM each time the tab key is pressed so you do not have to create an explicit list of focusable items within the modal window to keep track of</li>
                        <li>The escape key is mapped to the function to close the modal window</li>
                        <li>The enter key is mapped to the submit function of the modal window</li>
                    </ol>
                </li>
                <li>The title of the modal dialog is identified through the aria-labelledby attribute.</li>
                <li>The beginning of the modal dialog is marked with an h1</li>
                <li>There are offscreen instructions that describe the modal dialog and how to interact with it
                    <ol>
                        <li>The instructions are attached through the aria-describedby attribute.</li>
                        <li>In a previous version of this demonstration, the description was attached through the aria-labelledby attribute. It is more semantically correct to put the description/overview of the window in the aria-describedby attribute.</li>
                        <li>JAWS, NVDA, and ChromeVox announce both the aria-labelledby and aria-describedby attributes when the modal dialog opens.</li>
                        <li>VoiceOver only announces the aria-labelledby attribute, but users can still read the instructions via the virtual cursor.</li>
                        <li>Orca does not announce either the aria-labelledby or aria-describedby attributes, but users can still read the title and instructions via the virtual cursor.</li>
                    </ol>
                </li>
                <li>This configuration also wraps the entire inner contents of the modal dialog with a role=&quot;document&quot;. This allows all screen reader users the ability to use their virtual cursor inside of the modal dialog. This facilitates navigating more complex modal dialogs. This is optional and only necessary if you have elements in the modal dialog that are not focusable or are not labels of focusable elements.
                    <ul>
                        <li>All of the screen readers except for NVDA already allow for the virtual cursor to be used in a role=&quot;dialog&quot;, <strong>however</strong>, this goes against the W3C spec. A dialog should be considered an application, and all users should only be able to navigate to focusable items, unless an alternative navigation method is provided by the page. </li>
                        <li>Providing this functionality allows NVDA users to also have this feature.</li>
                    </ul>
                </li>
                <li>Note, there used to be a shim in place to handle a support problem in VoiceOver. In OS X 10.9 this bug has been fixed and the shim is no longer needed.</li>
                <li>Configurations Tested
                    <ul>
                        <li>JAWS 15.0.4203 in IE 11.0.9600.16428 in Windows 7, Service Pack 1: <strong>Passed</strong></li>
                        <li>NVDA 2013.3 in Firefox 26.0 in Windows 7, Service Pack 1: <strong>Passed</strong></li>
                        <li>VoiceOver in Safari 7.0 (9537.71) in OS X 10.9.0: <strong>Passed - although aria-describedby is not read automatically</strong></li>
                        <li>ChromeVox 1.31.0 in Chrome 31.0.1650.63 in OS X 10.9.0: <strong>Passed</strong></li>
                        <li>Orca 3.4.2 in Firefox 26.0 in Ubuntu 12.04: <strong>Partial Functionality - does not support aria-hidden and does not announce the title of the window</strong></li>
                    </ul>
                </li>
            </ol>
        </div>
        <div id="modal" aria-hidden="true" aria-labelledby="modalTitle" aria-describedby="modalDescription" role="dialog">
            <div role="document"> <!-- Optional role if you want screen reader users to be able to interact with objects other than focusable elements and their labels.  -->
                <div id="modalDescription" class="screen-reader-offscreen">Beginning of dialog window. It begins with a heading 1 called &quot;Registration Form&quot;. Escape will cancel and close the window. This form does not collect any actual information.</div>
                <h1 id="modalTitle">Registration Form</h1>
                <p>These are the onscreen instructions that are not attached explicitly to a focusable element. Can screen reader users read this text with the virtual cursor?</p>
                <form name="form1" method="post" action="">
                    <p>
                        <label for="firstName">First Name</label>
                        <input type="text" name="firstName" id="firstName">
                    </p>
                    <p>
                        <label for="lastName">Last Name</label>
                        <input type="text" name="lastName" id="lastName">
                    </p>
                    <p>
                        <label for="email">Email Address</label>
                        <input type="text" name="email" id="email">
                    </p>
                    <p>
                        <input type="button" name="button" id="enter" value="Submit">
                        <input type="button" name="cancelButton" id="cancelButton" value="Cancel">
                    </p>
                    <a id="modalCloseButton" class="modalCloseButton" href="javascript:void(0)" title="Close registration form"><img id="cancel" src="x.png" alt="close the registration form"></a>

                </form>
            </div> <!-- Matching div for optional role="document" -->
        </div>
        <div id="modalOverlay" tabindex="-1"></div>

            
          
!
            
                          #modalOverlay {
                width:100%;
                height:100%;
                z-index:2; /* places the modal overlay between the main page and the modal dialog*/
                background-color:#000;
                opacity:0.5;
                position:fixed;
                top:0;
                left:0;
                display:none;
                margin:0;
                padding:0;
            }

            #modal {
                width:50%;
                margin-left:auto;
                margin-right:auto;
                padding: 5px;
                border: thin #000 solid;
                background-color:#fff;
                z-index:3; /* places the modal dialog on top of everything else */
                position:fixed;
                top:25%;
                left:25%;
                display:none;
            }
            #modal h1 {
                text-align:center;
            }

            .modalCloseButton {
                float:right;
                position:absolute;
                top:10px;
                left:95%;
            }
            .modalCloseButton img {
                border:0;
            }

            .screen-reader-offscreen {
                position:absolute;
                left:-999px;
                width:1px;
                height:1px;
                top:auto;
            }

            
          
!
            
              /*
 
 ============================================
 License for Application
 ============================================
 
 This license is governed by United States copyright law, and with respect to matters
 of tort, contract, and other causes of action it is governed by North Carolina law,
 without regard to North Carolina choice of law provisions.  The forum for any dispute
 resolution shall be in Wake County, North Carolina.
 
 Redistribution and use in source and binary forms, with or without modification, are
 permitted provided that the following conditions are met:
 
 1. Redistributions of source code must retain the above copyright notice, this list
 of conditions and the following disclaimer.
 
 2. Redistributions in binary form must reproduce the above copyright notice, this
 list of conditions and the following disclaimer in the documentation and/or other
 materials provided with the distribution.
 
 3. The name of the author may not be used to endorse or promote products derived from
 this software without specific prior written permission.
 
 THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 */

// jQuery formatted selector to search for focusable items
var focusableElementsString = "a[href], area[href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), button:not([disabled]), iframe, object, embed, *[tabindex], *[contenteditable]";

// store the item that has focus before opening the modal window
var focusedElementBeforeModal;

$(document).ready(function() {
    jQuery('#startModal').click(function(e) {
        showModal($('#modal'));
    });
    jQuery('#cancel').click(function(e) {
        hideModal();
    });
    jQuery('#cancelButton').click(function(e) {
        hideModal();
    });
    jQuery('#enter').click(function(e) {
        enterButtonModal();
    });
    jQuery('#modalCloseButton').click(function(e) {
        hideModal();
    });
    jQuery('#modalCloseButton').keydown(function(event) {
        trapSpaceKey($(this), event, hideModal);
    })
    jQuery('#modal').keydown(function(event) {
        trapTabKey($(this), event);
    })
    jQuery('#modal').keydown(function(event) {
        trapEscapeKey($(this), event);
    })

});


function trapSpaceKey(obj, evt, f) {
    // if space key pressed
    if (evt.which == 32) {
        // fire the user passed event
        f();
        evt.preventDefault();
    }
}

function trapEscapeKey(obj, evt) {

    // if escape pressed
    if (evt.which == 27) {

        // get list of all children elements in given object
        var o = obj.find('*');

        // get list of focusable items
        var cancelElement;
        cancelElement = o.filter("#cancel")

        // close the modal window
        cancelElement.click();
        evt.preventDefault();
    }

}

function trapTabKey(obj, evt) {

    // if tab or shift-tab pressed
    if (evt.which == 9) {

        // get list of all children elements in given object
        var o = obj.find('*');

        // get list of focusable items
        var focusableItems;
        focusableItems = o.filter(focusableElementsString).filter(':visible')

        // get currently focused item
        var focusedItem;
        focusedItem = jQuery(':focus');

        // get the number of focusable items
        var numberOfFocusableItems;
        numberOfFocusableItems = focusableItems.length

        // get the index of the currently focused item
        var focusedItemIndex;
        focusedItemIndex = focusableItems.index(focusedItem);

        if (evt.shiftKey) {
            //back tab
            // if focused on first item and user preses back-tab, go to the last focusable item
            if (focusedItemIndex == 0) {

                focusableItems.get(numberOfFocusableItems - 1).focus();
                evt.preventDefault();
            }

        } else {
            //forward tab
            // if focused on the last item and user preses tab, go to the first focusable item

            if (focusedItemIndex == numberOfFocusableItems - 1) {
                debugger;
               focusableItems.get(0).focus();
                evt.preventDefault();
            }
        }
    }

}

function setInitialFocusModal(obj) {
    // get list of all children elements in given object
    var o = obj.find('*');

    // set focus to first focusable item
    var focusableItems;
    focusableItems = o.filter(focusableElementsString).filter(':visible').first().focus();

}

function enterButtonModal() {
    // BEGIN logic for executing the Enter button action for the modal window
    alert('form submitted');
    // END logic for executing the Enter button action for the modal window
    hideModal();
}

function showModal(obj) {
    jQuery('#mainPage').attr('aria-hidden', 'true'); // mark the main page as hidden
    jQuery('#modalOverlay').css('display', 'block'); // insert an overlay to prevent clicking and make a visual change to indicate the main apge is not available
    jQuery('#modal').css('display', 'block'); // make the modal window visible
    jQuery('#modal').attr('aria-hidden', 'false'); // mark the modal window as visible

    // save current focus
    focusedElementBeforeModal = jQuery(':focus');

    // get list of all children elements in given object
    var o = obj.find('*');

    // Safari and VoiceOver shim
    // if VoiceOver in Safari is used, set the initial focus to the modal window itself instead of the first keyboard focusable item. This causes VoiceOver to announce the aria-labelled attributes. Otherwise, Safari and VoiceOver will not announce the labels attached to the modal window.

    // set the focus to the first keyboard focusable item
    o.filter(focusableElementsString).filter(':visible').first().focus();


}

function hideModal() {
    jQuery('#modalOverlay').css('display', 'none'); // remove the overlay in order to make the main screen available again
    jQuery('#modal').css('display', 'none'); // hide the modal window
    jQuery('#modal').attr('aria-hidden', 'true'); // mark the modal window as hidden
    jQuery('#mainPage').attr('aria-hidden', 'false'); // mark the main page as visible

    // set focus back to element that had it before the modal was opened
    focusedElementBeforeModal.focus();
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console