Are HTML FORM Tags Required To Display Form Input Controls?

Posted October 26, 2012 at 3:45 PM by Ben Nadel

Tags: Javascript / DHTML, HTML / CSS

When I got into web development - before AJAX was a thing - I used HTML FORM tags where ever and when ever I needed to display a form input control. This was because all forms actually needed to be submitted back to the server for processing. In the last 10 years or so, I've never really questioned this approach. Even in AJAX (Asynchronous JavaScript and XML) applications, I religiously use FORM tags when ever I needed to display input controls. Recently, however, I worked with a front-end developer who used FORM tags only when a form submission was actually likely to take place. This gave me pause and lead to some crowd-sourced information gathering:


 
 
 

 
Are FORM tags required in order to show Input controls?  
 
 
 

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.

That said, it seems that using a FORM tag does have some benefits, depending on your particular situation:

  • You need it if you want to execute a traditional (ie. non-AJAX) form post.
  • You need it you want to capture the "submit" event programmatically.
  • You need it in order for mobile Safari to show the "Go" button on the keyboard.
  • You need it if you want to programmatically "reset" a form (ie. call reset()).
  • It makes generic form serialization easier (since it groups input fields).
  • You need it if you want to post files without the modern file API.
  • You need it if you have to segregate fields with the same name.

For me, the biggest "gotcha" in the list above is being able to listen for and react to the "submit" event. As far as I'm concerned, if you can't submit data by hitting the "Enter" key, then your form is broken. Of course, this is not always required. And, when it's not required, I have to same that I am pleased that the FORM tag is optional. I will now use it with discretion.



Reader Comments

Oct 26, 2012 at 4:23 PM // reply »
1 Comments

Another benefit to using the <form> tag, even if you are using AJAX, is that if the user has disabled JavaScript (I know, WHO DOES THAT?), the form could still submit. That would fall under "graceful degradation".


Oct 26, 2012 at 5:45 PM // reply »
270 Comments

It gives you the ability to do convenient document.formname.elementname references. Don't have to code id attributes and document.getElementById() all the time.

Less overhead to looping over a document.formname.elements array. Don't have to include 90K+ jquery.min.js just use $("input,select,textarea").each().

"A place for your stuff."


Oct 26, 2012 at 6:11 PM // reply »
1 Comments

I'm not sure a page with form controls (input, select, ...) but without a <form> is w3c valid...


Oct 26, 2012 at 6:29 PM // reply »
2 Comments

Another classic case of "HTML5 parser is very forgiving as people built shit in the past, this should not encourage you though to write shit now".

A form that doesn't work without JS to me still is a very arrogant and error prone idea. Yes, nobody turns off JS, but one of your scripts not loading or a broken "tweet this" button can still kill the JS execution on the page prematurely. Another thing a form of course allows you to do is send the data to a different script than the current page, which is what the action attribute is for or define a different encoding for the sent data and allowing you to add more header information.

I wrote the first Ajax book. When I read answers like these I regret this now.


Oct 27, 2012 at 5:15 PM // reply »
11,238 Comments

@Christian,

I think it really comes down to your particular circumstance. In today's age, people are building applications that depend on a JavaScript as a "platform". In the same way that you need the Flash plugin to run a FLEX app, people are building apps that absolutely require JavaScript to run. I don't think that's arrogant - I think that's simply a fact. It just comes down to what your audience is and what your business requirements are.

I don't think anyone here is advocating the writing of bad HTML simply because it parses.


Oct 29, 2012 at 10:42 AM // reply »
17 Comments

I definitely prefer including the form tags simply because I'm more comfortable binding to the submit event, and pressing enter to submit a form is second nature for someone who uses forms often or in an application with many forms.

Sure, I could add a key event to catch the enter key, but that feels like the wrong way to fix it when there is little to no downside to adding the form tags to begin with.


Oct 29, 2012 at 2:04 PM // reply »
2 Comments

I'm thinking there's a Section 508 / Accessibility red flag on that play.


Oct 31, 2012 at 11:16 AM // reply »
1 Comments

The <form> tag is also helpful if you want to quickly serialize the items in the form with jQuery.serialize(), possibly with an AJAX call.


Oct 31, 2012 at 11:50 AM // reply »
270 Comments

@Ben,

I'm sure you got into the habit of always coding a form tag because of Netscape. Early versions of NS used to refuse to render form elements on the screen if they weren't in a form.

Just for jollies, I wrote a little tester page and brought it up in NS 8.1. (Yes, I still have it. Doesn't everyone?) You may be happy to know, at least by 8.1, even good old Netscape started imaging form elements that weren't in a form.

I also snooped around the DOM a bit. In every browser I tried, form elements that aren't in a form have their form property defined as null. None of them, not even NS, defined an implicit form node just to hold the form element nodes. (Browsers sometimes define implicit DOM nodes to normalize the DOM, but not in this case.) The only form nodes in the DOM are those explicitly defined with form tags.

So that reminded me of yet another reason to define a form tag: so you can say this.form.otherelementname in onchange scripts and the like.


Ben
Nov 6, 2012 at 7:54 AM // reply »
3 Comments

AS a web developer for about 18 years, I've been in the habit of using the form tag around form elements. Seems like long ago, but I do recall browsers not displaying forms not surrounded by the form tag. This is an interesting article, but I didn't read anything that would change my habit of always using the form tag.


Nov 8, 2012 at 6:50 AM // reply »
1 Comments

Interesting post and feedback! Your intro grabbed my attention, because a form should be contained in the appropriate <form> tag. (it makes me sleep better at night knowing my code is semantically correct).

I remember reading post after post going on and on about semantic HTML, separation of concerns when CSS was introduced, etc. I admit we've come a long way since then, but i don't think we should completely abandon those values.

I am of the opinion that a form should be enclosed in the <form> tag, regardless of the transportation system used to communicate with your server. It just seems "right", not to mention fair to the semantic evangelists of old :)

If an element is needed somewhere in your markup that visually looks like a button, but doesn't have anything to do with a form or submitting any data then it's called an anchor. With minimal CSS, that anchor can be styled to look like a button. Your users are non the wiser and the elements' intent will be far clearer for to developer working with that code after you're gone.

Just because something is not required for a task to be accomplished, doesn't mean it shouldn't be included. The code commenting debate comes to mind. Should you, shouldn't you... But we all know it's just better if you do :)

Again, interesting post. Thanks!


Nov 8, 2012 at 12:55 PM // reply »
63 Comments

This is very interesting ... Even HTML 4 strict inputs validate without form tags.

https://gist.github.com/4040378

http://validator.w3.org/check


Nov 13, 2012 at 9:53 AM // reply »
4 Comments

Ben, I'm doing a lot of heavy usage of AJAX and for the most part I don't use the FORM tag anymore.

I have the benefit of not having to worry about which browser, or ancient browsers, etc., because it is an internal company web site and the company has set a standard for which web browser will be used. That web browser is installed by default on corporate machines.

I'm trully glad for this turn of events, I've noticed in the past how using the FORM tag on IE would shift its internal contents slightly down and to the right which could be a pain when trying to get one's page layout working just right!

I've written my own AJAX library since I found what was out there overly complex (at least to me), and am happy with the results. I use it along with JQuery. My libary automatically submits data as form posts so I am not limited in the size of my blocks.

There is one case where I would still use a FORM tag, when I want to post to a new pop-up window (for opening up a new Excel sheet based on posted data for example). Here I use the deprecated TARGET attribute. It may not be the totally best ways to do this, but it works in a pinch.


Dec 14, 2012 at 9:14 AM // reply »
1 Comments

I hope everything is okay. I don't see new posts. Always enjoyed your wisdom. Please keep posting


Jan 11, 2013 at 8:47 PM // reply »
11,238 Comments

@Alan,

Thanks a lot my man! Unfortunately, I've been buried under a deadline for the last few months and I've had to give up most of the things in my life that make me happy :D (ok, maybe a little overly-dramatic ... but, it's been rough). Hopefully I'll be back in the game starting next week. In the last month, I've managed like 1 post a month; but I should have a lot more coming down the pipe.

Thanks for checking in - I really appreciate it!



Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 17, 2013 at 7:42 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
Ben - thanks so much for posting these Angular articles and findings, they've been a huge help towards learning one of the more 'complex' JavaScript frameworks out there (IMO). I have been using Angu ... read »
May 16, 2013 at 5:01 PM
UPDATE: Parsing CSV Data Files In ColdFusion With csvToArray()
Your code was the closest thing I've found to obtaining some direction for converting ISO fields to values that CF can translate properly. Thank you for posting! ... read »
May 15, 2013 at 10:37 PM
Very Simple Pusher And ColdFusion Powered Chat
hi id making plz easy ... read »
May 15, 2013 at 6:07 PM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
Ben, you once again saved my bacon at work. Thank you, thank you, thank you! ... read »
May 15, 2013 at 4:15 PM
What If All User Interface (UI) Data Came In Reports?
@Josh, Thanks! @Ben, I definitely recommend the David West book "Object Thinking" I've been quoting from. It goes deeply into the philosophy and history of OO programming. His breadth ... read »
May 15, 2013 at 11:36 AM
Ask Ben: Print Part Of A Web Page With jQuery
I found this helpfull when you need to keep (refresh) the original parent page after closing the iframe child print dialog (Hoping you're not using a form at this time so it won't submit again): On ... read »
May 14, 2013 at 7:13 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, If there's any books you'd recommend on the subject of domain modelling, I'd love to hear it. I just downloaded the free PDF of "Domain Driven Design Quickly". Figured I'd give it ... read »
May 14, 2013 at 6:57 PM
The UX Of Prototyping: Low-Fidelity Is The New High-Fidelity
@Phillip, I'm not sure I follow what you mean? Are you saying that you looked at the list of widgets provided by the jQuery UI and let that be your style guide? ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools