Fieldset Tag in HTML5

Introduction

 
The <fieldset> is a useful tool for organizing and grouping related items within a HTML form. The <fieldset> tag is supported in all major browsers. It has the effect of creating a box around the grouped items and showing a description to the right of each item. By using the <fieldset> tag and the <legend> tag, you can make your forms much easier to understand for your users. The <legend> tag defines a caption for the fieldset element.
 
Syntax
 
This element uses separate opening and closing tags.
 
<fieldset>...</fieldset>

 

Attributes

 
HTML tags can contain one or more attributes. Attributes are added to a tag to provide the browser with more information about how the tag should appear or behave. Attributes consist of a name and a value separated by an equals (=) sign, with the value surrounded by double-quotes. There are 3 kinds of attributes that you can add to your HTML tags: Element-specific, global, and event handler content attributes.
 

Element-Specific Attributes

 
The following table shows the attributes that are specific to this tag/element.
 
Attributes Introduced by HTML5
 
Attributes Description             
disabled Disables all form control descendants of the <fieldset> element.
form Specifies a form to associate this <fieldset> element with. The value must be an ID of a form element.
name Specifies the name of the <fieldset>.
 

Global Attributes

 
The following attributes are standard across all HTML 5 tags.
 
HTML5 Global Attributes
accesskey draggable style
class hidden tabindex
dir spellcheck  
contenteditable id title
contextmenu lang  
 

Event Handler Content Attributes

 
Event handler content attributes enable you to invoke a script from within your HTML. The script is invoked when a certain "event" occurs. Each event handler content attribute deals with a different event. Here are the standard HTML 5 event handler content attributes.
 
onabort onerror* onmousewheel
onblur* onfocus* onpause
oncanplay onformchange onplay
oncanplaythrough onforminput onplaying
onchange oninput onprogress
onclick oninvalid onratechange
oncontextmenu onkeydown onreadystatechange
ondblclick onkeypress onscroll
ondrag onkeyup onseeked
ondragend onload* onseeking
ondragenter onloadeddata onselect
ondragleave onloadedmetadata onshow
ondragover onloadstart onstalled
ondragstart onmousedown onsubmit
ondrop onmousemove onsuspend
ondurationchange onmouseout ontimeupdate
onemptied onmouseover onvolumechange
onended onmouseup onwaiting
 
For example
 
a form might contain a few fields about name and email, some fields asking for opinions, and a field for "other comments". <FIELDSET> could be used to group those fields.
  1. <html>  
  2.     <head >This is the example of the fieldset tag   
  3. </head>  
  4.     <body>  
  5.         <form>  
  6.             <FIELDSET>  
  7. name:   
  8.                 <INPUT NAME="realname">  
  9.                     <BR>  
  10. email:   
  11.                         <INPUT NAME="email">  
  12.                         </FIELDSET>  
  13.                         <p>  
  14.                             <FIELDSET>  
  15. favorite color:   
  16.                                 <INPUT NAME="favecolor">  
  17.                                     <BR>  
  18.                                         <INPUT TYPE=CHECKBOX NAME="onions"> like green onions  
  19.                                             <BR>  
  20.                                                 <INPUT TYPE=CHECKBOX NAME="cookies"> like cookies  
  21.                                                     <BR>  
  22.                                                         <INPUT TYPE=CHECKBOX NAME="kimchee"> like kim chee  
  23.                                                             <BR>  
  24.                                                             </FIELDSET>  
  25.                                                             <P>  
  26.                                                                 <FIELDSET>  
  27. other comments:  
  28.                                                                     <BR>  
  29.                                                                         <TEXTAREA NAME="comments" ROWS=5 COLS=25></TEXTAREA>  
  30.                                                                     </FIELDSET>  
  31.                                                                 </form>  
  32.                                                             </body>  
  33.                                                         </html>  
FireFox  
 
fieldset2.gif
Figure1
 
Internet Explorer
 
fieldset3.gif
Figure2 
 

Now using legend tag

 
The <legend> tag defines a caption for the fieldset element.
 
For example
 
In the above example, we define the caption for the fieldset element.
  1. <html>  
  2.     <head>This is the example of the fieldset tag</head>  
  3.     <body>  
  4.         <form>  
  5.             <FIELDSET>  
  6.                 <legend >Personal information</legend>  
  7. name:   
  8.                 <INPUT NAME="realname"  
  9.                     <BR>  
  10.                         <br />  
  11. email:   
  12.                         <INPUT NAME="email">  
  13.                         </FIELDSET>  
  14.                         <FIELDSET>  
  15.                             <legend >Opinions fields field</legend>  
  16. favorite color:   
  17.                             <INPUT NAME="favecolor">  
  18.                                 <BR>  
  19.                                     <INPUT TYPE=CHECKBOX NAME="onions"> like green onions  
  20.                                         <BR>  
  21.                                             <INPUT TYPE=CHECKBOX NAME="cookies"> like cookies  
  22.                                                 <BR>  
  23.                                                     <INPUT TYPE=CHECKBOX NAME="kimchee"> like kim chee  
  24.                                                         <BR>  
  25.                                                         </FIELDSET>  
  26.                                                         <P>  
  27.                                                             <FIELDSET>  
  28.                                                                 <legend >Comment field</legend>  
  29. other comments:  
  30.                                                                 <BR>  
  31.                                                                     <TEXTAREA NAME="comments" ROWS=5 COLS=25></TEXTAREA>  
  32.                                                                 </FIELDSET>  
  33.                                                             </form>  
  34.                                                         </body>  
  35.                                                     </html>   
Internet Explorer
 
This defines the fields with a caption.
 
fieldset4.gif
Figure3 
 

New for the <fieldset> tag in HTML5

 
The below attributes are the new attribute in HTML5.
 

1. disabled Attribute

 
 To disabled attribute specifies that a group of form elements (a fieldset) should be disabled. A disabled fieldset is unusable and un-clickable.
 
Syntax
 
 <fieldset disabled="disabled">
 
For example
  1. <FIELDSET disabled="disabled">  
  2.     <legend >Personal information</legend>  
  3. name:   
  4.     <INPUT NAME="realname"  
  5.         <BR>  
  6.             <br />  
  7. email:   
  8.             <INPUT NAME="email">  
  9.             </FIELDSET>  
Internet Explorer
 
fieldset5.gif
Figure4

 
2. Form Attribute

  1. The form attribute specifies one or more forms the fieldset belongs to.
  2. The value of the form attribute must be the id of the form it belongs to.
  3. To refer to more than one form, use a space-separated list.
For example
  1. <html>  
  2.     <head>This is the example of the fieldset tag</head>  
  3.     <body>  
  4.         <form action="demo_form.asp" method="get" id="ice">  
  5.   
  6. What is your favorite ice cream flavor?   
  7.             <input type="text" name="icecream" />  
  8.             <br />  
  9.             <input type="submit" />  
  10.         </form>  
  11.         <p>The fieldset below is outside the form element, but still part of the form.</p>  
  12.         <FIELDSET form="ice">  
  13.             <legend >Personal information</legend>  
  14.   
  15. name:   
  16.             <INPUT NAME="realname"  
  17.                 <BR>  
  18.                     <br />  
  19.   
  20. email:   
  21.                     <INPUT NAME="email">  
  22.                     </FIELDSET>  
  23.                 </form>  
  24.             </body>  
  25.         </html>  
Internet Explorer
 
fieldset6.gif
Figure5

 

3. name Attribute

 
The name attribute specifies the name of a fieldset.
 
Syntax
 
<fieldset name="value">
<FIELDSET name="person">
<legend >Personal information</legend>
name: <INPUT NAME="realname"<BR>
    <br />
email: <INPUT NAME="email">
</FIELDSET>