Frontend Master

JavaScript Curriculum

Forms and Inputs
+45 XP

Forms and Inputs

easy
~26 min·45 XP

The coffee shop wants an online order form. You'll wire up text fields, a dropdown for drink size, a checkbox for extras, and a submit button — all properly labelled and validated.

Forms and Inputs

Forms are how the web collects information from users. Every login screen, search bar, checkout page, and survey is built with the same handful of HTML elements.

The <form> Element

A <form> wraps all your inputs and tells the browser where to send the data.

html
<form action="/submit" method="post"> <!-- inputs go here --> </form>
  • action — the URL that receives the data (omit for same page)
  • methodget appends data to the URL; post sends it in the request body

Input Types

The <input> element is the workhorse of forms. Its type attribute controls everything:

form-elements-explorer.html
Live Preview
Description

Single-line plain text

Mobile Behaviour

📱 Default keyboard on mobile

HTML
<input type="text" />

Labels — Always Use Them

Every input needs a visible label. Labels improve usability and accessibility.

label-input-linker.html

Label's for attribute matches input's id. Label can be anywhere in the DOM.

<label for="username">Username</label>
<input type="text" id="username">
Screen reader announces:"Username", edit text

Most common pattern. Works even when label and input are far apart in markup.

Validation Attributes

HTML gives you free client-side validation — no JavaScript required:

form-validation.html
Toggle Constraints

Field must not be empty

<input type="text" required />
Try It

Other Form Elements

html
<!-- Multi-line text --> <textarea name="message" rows="4"></textarea> <!-- Dropdown --> <select name="country"> <option value="in">India</option> <option value="us">United States</option> </select> <!-- Submit button --> <button type="submit">Send</button>

Key Rules to Remember

  1. Always wrap inputs in a <form>
  2. Every input needs a <label> linked via for/id
  3. Use the right type — it gives free validation and the correct mobile keyboard
  4. Add name attributes — without them the data won't be sent
  5. Use required, minlength, pattern for browser validation

Challenge: Build a sign-up form with name, email, password (min 8 chars), a terms checkbox, and a submit button. All inputs must have proper labels.

Challenge

Build a sign-up form with a name field, email field, password field (minlength 8), a terms-of-service checkbox, and a submit button. All inputs must have properly linked labels.

forminputlabelbuttonselecttextareavalidationrequiredminlengthpattern
Forms and Inputs | Nexus Learn