Pen Settings

HTML

CSS

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

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
<html>
<head>
  <title>Trap focus using javaScript</title>
</head> 
<body>
  <div class="container">
    <h1 class="text-center">Trap focus using javaScript</h1>
    <div class="input-wrapper">
      <input type="text" placeholder="Input 1" value="">
      <input type="text" placeholder="Input 2" value="">
      <input type="text" placeholder="Input 3" value="">
      <input type="text" placeholder="Input 4" value="">
      <input type="text" placeholder="Input 5" value="">
      <button onclick="alertMessage()">Submit</button>
    </div>
  </div>
</body>
</html>
              
            
!

CSS

              
                html{
  background: black;
}
.container{
  background: yellow;
  width: 100%;
  max-width: 500px;
  margin: auto;
  border-radius: 10px;
  
}

h1{
  padding-top: 0.4em;
}

.input-wrapper{
  background: pink;
  padding: 1em;
  display: flex;
  flex-direction: column;
  border-radius: 0 0 10px 10px;
}

.input-wrapper input{
  margin: 0.4em auto;
  padding: 0.4em;
  font-size: 1.4em;
  width: 400px
}


.text-center{
  text-align: center;
}

button{
  width: 100px;
  padding: 0.4em;
  border-radius: 4px;
  margin: auto;
  font-size: 1.2em;
  cursor: pointer;
}
              
            
!

JS

              
                function alertMessage(){
  alert("Yey you learnt something new ;)");
}

document.addEventListener('keydown',handleInputFocusTransfer);

function handleInputFocusTransfer(e){
  // This method move focus to next item input on DOWN or to previous item input on UP arrow key press.
  const focusableInputElements= document.querySelectorAll(`input`);
  
  //Creating an array from the node list
  const focusable= [...focusableInputElements]; 
  
  //get the index of current item
  const index = focusable.indexOf(document.activeElement); 
  
  // Create a variable to store the idex of next item to be focussed
  let nextIndex = 0;

  if (e.keyCode === 38) {
    // up arrow
    e.preventDefault();
    nextIndex= index > 0 ? index-1 : 0;
    focusableInputElements[nextIndex].focus();
  }
  else if (e.keyCode === 40) {
    // down arrow
    e.preventDefault();
    nextIndex= index+1 < focusable.length ? index+1 : index;
    focusableInputElements[nextIndex].focus();
  }
}
              
            
!
999px

Console