What's the point of HTML forms `name` attribute?

In short, and probably oversimplifying a bit: It is used instead of id for browsers that don't understand document.getElementById.

These days it serves no real purpose. It is a legacy from the early days of the browser wars before the use of name to describe how to send control values when a form is submitted and id to identify an element within the page was settled.


From the spec:

The name attribute represents the form's name within the forms collection.


Once you assign a name to an element, you can refer to that element via document.name_of_element throughout your code. Doesn't work too tell when you've got multiple fields of the same name, but it does allow shortcuts like:

<form name="myform" ...>

document.myform.submit();

instead of

document.getElementsByName('myform')[0].submit();

Tags:

Html

Forms