Lesson 5

Forms in HTML


The Input Element: The Box for Your Stuff

This is the most common form tag. It's a self-closing tag that creates an input field. Its behavior changes based on its type attribute. The most basic type is text.

input code png

The Need for Meaning - The label

The Problem: We have a box, but the user has no idea what they are supposed to type into it. Is it for a name? An email? A search query? The box is meaningless without a description.

The Solution: We need to add a descriptive piece of text. The correct HTML tag for this is the label. It's a tag specifically designed to be the title for a form field.

Let's add a label:
label code img

Result: This is better! Now the user sees "First Name:" next to the box and knows what to type. But the label and the input are still two completely separate, unrelated things. The browser doesn't know they belong together


The Need for Connection - id and for

The Problem: How can we create a direct, unbreakable link between the label "First Name:" and its specific input box? We need this for two reasons:

  1. Usability: It would be great if a user could click on the text of the label to activate the input box.
  2. Accessibility: Screen readers for visually impaired users need to know which label describes which input so they can announce it correctly.

The Solution: We need a unique naming system.

  1. First, we give our input box a unique name that no other element on the page has. The attribute for a unique name is id. Let's give it an id of "firstName".
  2. Next, we tell the label which element it is for. The for attribute on the label must match the id of the input.
Let's connect them:
id/for code

Result: We have now created a powerful, explicit connection.

  • Try it: If you click on the text "First Name:", your cursor will magically jump into the text box.
  • Behind the scenes: A screen reader will now announce, "First Name, edit text" when the user focuses on the input box. The two elements are now a true pair.

The Need for Submission - The form and submit

The Problem: We have a field for the user to fill out, but we have no way for them to actually submit this information. We need a container for our fields and a "Go" button.

The Solution:

  1. We wrap all our form fields in a form tag. This tag acts as the main container that tells the browser, "Everything inside here is part of one single submission."
  2. We add a button that tells the form to submit. The simplest way is input type="submit".
Let's build the form structure:
submit code png




Result:We now have a complete visual form with two fields and a submit button. When you click the button, the page reloads, but the data doesn't go anywhere yet.


The Need for Data Identification - The name Attribute

The Problem: When the form is submitted, the browser needs to package the data to send to a server. How does it label the data? If a user types "Arjun" in the first box, how does the server know that "Arjun" is themfirstName? The id attribute is only for use within the page; it is not sent to the server.

The Solution: We need another attribute whose sole purpose is to be the "data label" or the "key" for the submitted value. This is the name attribute.

Let's add names to our inputs:
name attribute img




Result: Now we have a truly functional form, ready to send meaningful data. When submitted, the browser will create a package that looks like this:

  • firstName = (whatever the user typed)
  • lastName = (whatever the user typed)