Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 autofocus lost on one-page sites

Tags:

html

autofocus

I wonder if someone has the answer to this dilemma? When one has autofocus set on an input element which is not in the page's landing section, autofocus is lost when a user click's a menu tag to go to that section. (e.g. a contact section where there is likely to be a form) ...

like image 575
JiminSA Avatar asked Sep 08 '14 11:09

JiminSA


People also ask

What does the html5 autofocus attribute do when applied to the HTML select element?

The autofocus attribute is a boolean attribute. When present, it specifies that the drop-down list should automatically get focus when the page loads.

What does the autofocus attribute do?

The autofocus global attribute is a Boolean attribute indicating that an element should be focused on page load, or when the <dialog> that it is part of is displayed. No more than one element in the document or dialog may have the autofocus attribute. If applied to multiple elements the first one will receive focus.

How will you focus on the input field when the page is loaded?

The autofocus attribute is a boolean attribute. When present, it specifies that an <input> element should automatically get focus when the page loads.

What is HTML autofocus button?

The autofocus attribute is a boolean attribute. When present, it specifies that a button should automatically get focus when the page loads.


1 Answers

The autofocus attribute -

lets you specify that a form control should have input focus when the page loads, unless the user overrides it, for example by typing in a different control. Only one form element in a document can have the autofocus attribute, which is a Boolean.

Is designed to prompt user agents to give initial focus to a form field when the page loads, providing a less "jarring" and more controllable experience to users of assistive technologies than previous JavaScript based solutions - which often "stole" focus after the user had already began to navigate the document.

The problems with using this attribute in your case are two fold-

  1. The page load event will only fire once, and does not fire again when you move focus to a new "page" internally within the same document

  2. It is invalid to have more than one element within the same document - in this case your entire single page document - which has the autofocus element.

What you need to do is use the native JavaScript focus method on a form element of your determination within the new viewport area.

If you have a reference to the new "page" element or container, you could use a class or data-attribute to specify the desired element by using the querySelector method or jQuery.

Something like-

var firstElement = pageContainer.querySelector('.focus-field');

if (firstElement) { 
    firstElement.focus();
}

Alternatively you could simply use an element selector to select the first form element-

var firstElement = pageContainer.querySelector('input, select');

if (firstElement) { 
    firstElement.focus();
}

Be aware in this case that it is invalid to focus a hidden input (for hopefully obvious reasons), and if you wish to use this solution it might be best to use the querySelectorAll method and loop through - checking that the type is not hidden and breaking out of the loop on the first valid element to focus. Or simply use attribute selectors and only select inputs of type text.

If you are using jQuery you could use the not function to exclude hidden inputs from the selected elements.

like image 192
pwdst Avatar answered Nov 14 '22 22:11

pwdst