Search code examples
htmlformsuser-experience

HTML forms: focus on checkbox after completing text field


Look at this HTML form and notice how it has a checkbox between text fields:

screenshot1

I tried to fill it up with Chrome on mobile and noticed an unexpected problem: if the user types into a text field, and then moves to the next field by pressing the blue "done" button in an Android keyboard, focus will jump over the checkbox into the next text field. This would be fine, except for the fact that the user never even sees the checkbox and thus may leave it unintentionally unchecked:

screenshot2

Is it possible to force focus on the checkbox field or something like that? If there is no technical solution, I'll be happy to accept a UX solution.

<form action="/action_page.php" method="get">
  <input name="text1" value="Some text"><br>
  <input name="text2" value="More text"><br>
  <input name="text3" value="Bla bla"><br>
  <input type="checkbox" name="vehicle" value="Bike"> I have a bike<br>
  <input name="text1" value="Some text"><br>
  <input name="text2" value="More text"><br>
  <input name="text3" value="Bla bla"><br>
  <input type="submit" value="Submit">
</form>


Solution

  • change Event & .focus()

    Try registering a change event on [name=text3] and trigger a .focus() on [name=vehicle]. When user has typed into [name=text3] then clicks elsewhere, a .focus() is triggered on [name=vehicle]

    Demo

    var blaBla = document.querySelector('[name=text3]');
    
    var bike = document.querySelector('[name=vehicle]');
    
    blaBla.addEventListener('change', goToChk);
    
    function goToChk(e) {
      bike.focus();
    }
    [name=vehicle]:focus {
      outline: 3px solid red;
    }
    <form action="/action_page.php" method="get">
      <input name="text1" value="Some text"><br>
      <input name="text2" value="More text"><br>
      <input name="text3" value="Bla bla"><br>
      <input type="checkbox" name="vehicle" value="Bike"> I have a bike<br>
      <input name="text1" value="Some text"><br>
      <input name="text2" value="More text"><br>
      <input name="text3" value="Bla bla"><br>
      <input type="submit" value="Submit">
    </form>