cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

            
              <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=EDGE">
    <title>Skip Link</title>
</head>
<body>

  <p>The article <a href="https://axesslab.com/skip-links/">Your skip links are broken | axesslab</a> includes a jQuery snippet to focus headings. This demo shows a vanilla JavaScript version of that technique; then two options you can use if you have an ID on the element(s) you want to skip to. See the JS tab for details. License included for those of you with compliance requirements.</p>

  <hr>
  <p>Demo:</p>
  <ul>
    <li><a id="skip-to-first-heading" href="#">Skip to first heading</a> (will go to site heading in this demo)</li>
    <li><a id="skip-link" href="#skip-target">Skip to #skip-target using IDs</a></li>
    <li><a id="skip-link2" href="#skip-target2">Skip to #skip-target2 (using function)</a></li>
    <li><a id="skip-link3" href="#skip-target3">Skip to #skip-target3 (using function)</a></li>
  </ul>
  <h1>Site Heading may be an H1</h1>    
  <h1 id="skip-target">Content Heading (#skip-target)</h1>
  <h2 id="skip-target2">Other heading (#skip-target2)</h2>
  <h2 id="skip-target3">Other heading (#skip-target3)</h2>
  <h3>Other heading</h3>
  <h4>Other heading</h4>
  <h5>Other heading</h5>
  <h6>Other heading</h6>

  <p>Note: IE11 in emulation mode claims these work back to IE9, if you have old browsers to support you should test more thoroughly.</p>
</body>
</html>
            
          
!
            
              body { color: black; background: white; font-family: sans-serif; padding: 1rem; }

/* Just to show where focus is */
h1:focus, h2:focus, h3:focus { outline: 2px dotted black; }
            
          
!
            
              /* 
MIT License

Copyright (c) 2018 Ben Buchanan (https://200ok.com.au/)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

// OPTION 1 - a direct conversion of the jQuery from the article
// Click a link via its ID; then find the first heading of any kind and jump to that.
// If you want the second heading, change [0] to [1] when setting target.
// You could also trim down the lower heading levels if you know  they won't be first.
document.getElementById('skip-to-first-heading').addEventListener('click', function(e) {
  e.preventDefault();
  var target = document.querySelectorAll('h1, h2, h3, h4, h5, h6')[0];
  target.setAttribute('tabindex', "-1");
  target.focus();
});

// OPTION 2 - go to specific heading by ID 
// If you have an ID on both elements, may as well just use them; it's a little faster as it avoids walking the DOM to find all the headings.
document.getElementById('skip-link').addEventListener('click', function(e) {
  e.preventDefault();
  var target = document.getElementById('skip-target');
  target.setAttribute('tabindex', '-1');
  target.focus();
});

// OPTION 3 - reusable function
// If you have multiple skip links all with IDs, a re-usable function...
function skipLink(linkId, targetId) {
  var target = document.getElementById(targetId);
  document.getElementById(linkId).addEventListener('click', function(e) {
    e.preventDefault();
    target.setAttribute('tabindex', '-1');
    target.focus();
  });
}
// ...which you can call like this
skipLink('skip-link2', 'skip-target2');
skipLink('skip-link3', 'skip-target3');

            
          
!
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