Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML form tag with no attributes - any reason not to?

Tags:

html

forms

At risk of sounding like a fool - The html (html5) designers at our firm keep handing me functional mocks with form tags that have no attributes...

<form>
    <label for="name">Name</label>
    <input type="text" id="name" name="name">
</form>

Every time I see this I lose hope in humanity just a little more. But I don't know if I should round them all up and lecture them on form attributes and why we need them since I can't seem to find any documentation that says they are "required." By default I know the form tag will assume things, like method and action, but I prefer those attributes are in place rather than omitted. It's just how it's always been for those of us who have been writing HTML since it began.

So the general question is - Is there anything wrong (however you wish to define that) with using the form tag with no attributes? If your intended action is the current page and the intended method is get, then what argument is there for including versus excluding the default attributes? Or assigning it an id that you don't use?

I expected to see this:

<form id="search_form" action="" method="get">
    <label for="name">Name</label>
    <input type="text" id="name" name="name">
</form>
like image 490
Kai Qing Avatar asked Feb 14 '12 21:02

Kai Qing


People also ask

Is form tag necessary in HTML?

The HTML form tag is required when you want to collect information that visitors provide. For example, you may want to collect specific data from visitors, such as name, email address, and password. The HTML <form> tag is used to create a form.

Are form tags necessary?

Form tags are still necessary if you want to be able to submit the form without AJAX (which may be useful in the early stages of development).

Which one is not attributes of form tag?

Which of the following is not an attribute of <form> tag? The url attribute is not an attribute of <form> tag.

Is the form tag optional?

The consensus seems to be that FORM tags are no longer required when you simply want to render a form input control. If all your input field is doing is providing a UI (user interface) hook for interactions, then you can put the input fields into the HTML without including a FORM tag.


2 Answers

It will depend on the DOCTYPE you are validating against. For example in HTML 4.01 Transitional and Strict, the action attribute is required whereas the method attribute is optional.

In HTML 5 both attributes are optional.

like image 92
Darin Dimitrov Avatar answered Nov 02 '22 06:11

Darin Dimitrov


The biggest issue with not having an id and action attributes on a form is that if you have more than one form on the page it is very difficult to say which one is being referred to - in either client or server code.

As James Montagne mentions in his answer for HTML 4.01 transitional the action attribute is required.

As a general issue, I personally would always be explicit about the id, action and method - keeps these important attributes in the open and makes the point that they were not forgotten.

like image 30
Oded Avatar answered Nov 02 '22 06:11

Oded