Disable press tab button to next input field in web form

For example below, user can press tab button on the keyboard to go to next field. Can I disable this function?

<form action="/action_page.php">
  First name:<br>
  <input type="text" name="firstname">
  <br>
  Last name:<br>
  <input type="text" name="lastname">
  <br><br>
      <select name="gender">
        <option value="M">M</option>
        <option value="F">F</option>
      </select>
  <br><br>
  <input type="submit" value="Submit">
</form> 

Answers 1

  • Yes use keydown like this :

    document.querySelector('input').addEventListener('keydown', function (e) {
        if (e.which == 9) {
            e.preventDefault();
        }
    });
    

Related Articles