htmlautofocus

HTML5 autofocus without moving page


I have put an attribute of autofocus on an input field, but the form itself is about midway down the page. This means that when the page first loads the user is not seeing the instructions at the top of the page telling them what to do.

How can I enable autofocus on an input field so that the cursor is ready to start typing into that field, but without causing the page to scroll to that field on the page. Is it possible?


Solution

  • It's the default browser behaviour to scroll to the autofocus element. But, with a bit of code tweak, you can get your desired behaviour. I tried to simulate your requirement. Please have a look at the below code & try to run in your browser.

    Ignore the additional styles. They are just to support my simulation.

    Code Snippet

    CSS

    body, p {
      padding: 0;
      margin: 0;
    }
    
    input {
      padding: 5px;
    }
    
    #autofocus {
      left: 0;
      opacity: 0;
      position: absolute;
      top: 0;
      z-index: -1;
    }
    
    #contentWrapper {
      padding: 10px;
    }
    
    #contentWrapper form {
      margin-top: 600px;
    }
    
    #contentWrapper p:last-child {
      margin-top: 200px;
    }
    

    HTML

    <form id="autofocus">
      <input type="text" id="autofocusInit" autofocus>
    </form>
    <div id="contentWrapper">
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
      <h3>Autofocus element is 600px down below!</h3>
    
      <form>
        <h3>Let's focus here...</h3>
        <input type="text" id="autofocusTarget" placeholder="Autofocus here...">
      </form>
    
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    </div>
    

    JavaScript

    (function() {
      document.querySelector('#autofocusInit').addEventListener('keydown', function(e) {
        console.log(e);
        this.setAttribute('autofocus', false);
        document.querySelector('#autofocusTarget').focus();
      });
    })();
    

    This will do the trick.

    On the other hand, you can detect when the element is in viewport & then set .focus() on that element.

    Here, opacity: 0; is used instead of display: none; as autofocus cannot be applied if the element's display property is set to none. You can refer Mozilla/input for more information.

    Hope this helps.