Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default html form focus without JavaScript

People also ask

How do you set a default focus in HTML?

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

Can I set focus with CSS?

No, there isn't. In normal circumstances, a div can't receive the focus at all, and CSS lacks anything to set the focus.

What is Auto focus in HTML?

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

How do you make a form field mandatory?

1. Required attribute: If you want to make an input mandatory to be entered by the user, you can use the required attribute. This attribute can be used with any input type such as email, URL, text, file, password, checkbox, radio, etc. This can help to make any input field mandatory.


You can do it in HTML5, but otherwise, you must use JavaScript.

HTML5 allows you to add autofocus to your form element, eg:

<input type="text" name="myInput" autofocus />

This does work in browsers which support HTML5 (Or rather, browsers which support this particular part of HTML5) but as you know, not everybody can use it yet.


Something to be aware of ... if you set a focused form element, then anyone using Assisted Technology (AT) like a screen reader will need to back up to see menus and any other content that is before the focused field.

A preferred method, in my opinion , is to not set focus to any field, except a skip-link if its available. That gives them the option to skip into the pages content or read the page from the top down.


As others have said, without Javascript you can't guarantee a default field. An alternative option you might want to try, if you have multiple fields that a user might want to access is using the accesskey attribute. This will essentially mean a user can return to either of the fields instantly later during browsing, which may come in handy for users of screen readers, etc...

Wikipedias article on this subject is quite useful - http://en.wikipedia.org/wiki/Access_key