Handling commonly used keys

Hey ,

I'm thrilled to help you learn JavaScript. Unfortunately, you've landed on a page where you cannot access with your current purchase.

Please upgrade (use this link) access this content.

I'm super eager to help you learn more!

Handling commonly used keys

These 6 keys are commonly used on the web:

  1. Tab
  2. Arrow keys
  3. Spacebar
  4. Enter
  5. Escape
  6. Backspace

We’ll talk about the behavior of each key and the things you need to know about them.

Tab

Tab is the most important key on the web. It lets you cycle through elements you can focus on. These elements include:

  1. Links
  2. Buttons
  3. Form elements

Note: For some weird reason, Safari doesn’t allow you to cycle through links and buttons by default. You need to enable it by going to Settings > Advanced. Under Accessibility, check “Press tab to highlight each item on a webpage”.

Safari's menu to activate tab functionality.

When you tab into an element, the element gets a focus state. The default style for focus is the blue glow around the element.

shift + tab lets you move to the previous focusable element.

Shift + Tab cycles backwards.
Tab cycles focus through a link, a button and an input field.

You can change the focus style with :focus.

*:focus {
  outline: 2px solid green;
}
Focus style changes to a green solid outline

Arrow keys

You can use arrow keys to browse a website:

  • Down arrow key () nudges the scrollbar downwards
  • Up arrow key () nudges the scrollbar upwards
Arrow keys nudges a website up and down.
  • option + scrolls down a page on a Mac. The PageDn key does the same thing on Windows.
  • option + scrolls up a page on a Mac. The PageUp key does the same thing on Windows.
Option + arrow keys on mac lets you scroll the website by one page at a time
  • command + scrolls to the end of the website on Mac. The End key does the same thing on Windows.
  • command + scrolls to the top of the website on Mac. The Home key does the same thing on Windows.
Command + arrow keys on mac lets you scroll the website by one page at a time

If the website has a horizontal scrollbar, you can also use left ( ) and right () keys to nudge the scrollbar in their respective directions.

When you’re interacting with form elements, arrow keys lets you:

  1. choose a radio option
  2. choose an option in a select element
Selecting radio option
Choose options in a select element

Space

Space can be used to interact with elements. It can be used to select a checkbox.

Space used to select checkboxes.

It can be used to open a <select> menu. It can also be used to choose an option from the menu.

Space used to open a select element. It is also used to select an option.

If you have focus on a button element, pressing space on the button activates the button. This triggers the click event.

<button>Button</button>
const button = document.querySelector('button')

button.addEventListener('click', evt => {
  console.log ('Clicked!')
})
Space on button triggers a click event.

Finally, space can be used to scroll down the website by one page. shift + spacebar can be used to scroll up one page.

Spacebar used to scroll down or up.
Note: the space key didn't show in the keystroke recorder when I pressed shift + space.

Enter

Enter can interact with some elements, but it is usually used to confirm or submit things.

First, if you’re on a button, you can use Enter to activate the button. This triggers a click event as well.

Enter used to click a button.

Second, if you’re on a link, Enter can be used to navigate to the href of the link.

Enter used to navigate to a page.

Third, Enter can also be used to submit a form. This works only if:

  1. Your focus is INSIDE a form
  2. Your form has a submit button
  3. Focus is not in <textarea>
  4. Focus is not in an opened <select> menu
const form = document.querySelector('form')
form.addEventListener('submit', evt => {
  evt.preventDefault() // Prevents default behavior so we can console log
  console.log('Submit event triggered!')
})
Enter used to submit a form.

If the focus is on a <textarea>, enter creates a new line in the <textarea>. It does not submit the form.

Enter creates a new line on textarea.

If you are in a select element, you can use enter to confirm an option. (You cannot use enter to open the select menu).

Enter submits a form when used on the select element.
But enter can be used to select an option.

Escape

The escape key is used to quit a feature. For example, if you start a search with command + f (ctrl + f on PC), you can quit the search with Escape.

Escape is commonly used to quit a feature.

Backspace

Backspace is used for deleting things. You can use it to delete text in a form element like <input type="text"> and <textarea>.

Backspace used to delete text.

In Firefox, backspace also triggers the browser’s back button. shift + backspace triggers the forward button.

Backspace used to move back and forward in the brower's history.
Note: The backspace key did not show up in the keystroke recorder when I pressed shift + backspace

The “back button” behavior was a norm among browsers in the past. This functionality has been removed from Webkit-based browsers. If you want to navigate backwards or forwards, you can use the following:

  • Safari and Chrome on Mac:
    • Back button: Command +
    • Forward button: Command +
  • Edge and Chrome on Windows:
    • Back button: Alt +
    • Forward button: Alt +