Take the 2-minute tour ×

What's point of the name="" html attribute in <form> tags? Here it just says that:

The name attribute specifies the name of a form. The name attribute is used to reference elements in a JavaScript.

Why can't we use the id or class attribute to reference elements with Javascript?

What was the name tag intended for? Has it any real importance today?

share|improve this question
add comment

3 Answers

up vote 5 down vote accepted

The <form> name attribite pre-dates using IDs in JavaScript, therefore it is there to backwards compatibility. It also forms a collection document.forms which can be handy when parsing a page.

share|improve this answer
add comment

You can access form using name reference in javascript.
Before jquery was invented, javascript used to access DOM elements using name, as well as Ids. You can refer this for further clarifications.

And for other HTML elements, element name is used at server end, to retrieve POST parameters.

$test = $_POST['name'];
share|improve this answer
add comment

W3C says same here http://www.w3.org/TR/html401/interact/forms.html#h-17.3 name attribute is just for scripting. Yes, one can use id attribute for that and id's is commonly used with jQuery or like.

However, there is some things, that corresponds to what w3c states, you can do with name. One example is javascript document.forms['formname'] to access form element. Described here http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-40002357

share|improve this answer
add comment

Your Answer


