#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
JavaScript Fundamentals

Additional features in forms

Modern HTML forms can do far more than plain text boxes. Specialised input types, suggestion lists, autofill hints and the FormData object let you build forms that are quicker to fill in, work better on phones, and are easy to read from JavaScript. These features build on basic validation and need little or no extra code.

Input types that help users

Choosing the right type changes both the browser widget and the mobile keyboard:

TypeWhat the user gets
emailKeyboard with "@", basic format checking
telNumeric phone keypad on mobile
numberSpinner buttons, honours min, max and step
date, timeNative date and time pickers
rangeA slider between min and max
colorA colour picker returning a hex value
searchA text field that may show a clear button

Browsers that do not support a type simply fall back to a normal text field, so using them is safe.

Suggestions, hints and focus

A datalist attaches a list of suggestions to a text input through the list attribute. Users can pick one or type something else. The autocomplete attribute tells the browser which saved data fits the field, such as name, email, tel, new-password or postal-code, so autofill fills the right boxes. autofocus places the cursor in a field when the page loads; use it only once per page, because jumping focus can confuse screen reader users.

Reading a whole form with FormData

Instead of reading each input one by one, pass the form element to new FormData(). It collects every field that has a name attribute. You can loop over it, read single values with get, or turn it into a plain object.

<form id="booking">
  <input name="city" list="cities" autocomplete="address-level2">
  <datalist id="cities">
    <option value="Delhi"></option>
    <option value="Mumbai"></option>
    <option value="Bengaluru"></option>
  </datalist>
  <input name="guests" type="number" min="1" max="8" value="2">
  <input name="checkin" type="date">
  <input name="ref" type="hidden" value="home-page">
  <button>Book</button>
</form>

<script>
  const form = document.getElementById("booking");
  form.addEventListener("submit", (event) => {
    event.preventDefault();
    const data = new FormData(form);
    const booking = Object.fromEntries(data);
    console.log(booking); // { city: "...", guests: "2", checkin: "...", ref: "home-page" }
  });
</script>

Notice that guests comes back as the string "2". Every value from a form is text, so convert numbers with Number() or read input.valueAsNumber directly.

Disabled versus readonly

Both stop the user from editing, but they behave differently. A readonly field can still be focused and is included when the form is submitted. A disabled field is greyed out, skipped by the Tab key, and its value is not submitted or included in FormData. Developers often disable a field to "lock" it and then wonder why the server never received the value. If you need the value sent, use readonly or a hidden input.

Grouping and resetting

A fieldset with a legend groups related fields, such as a set of radio buttons, and gives screen readers a heading for the group. Setting disabled on the fieldset disables every control inside it at once. A button with type="reset", or calling form.reset(), returns all fields to their initial values.

Takeaway: pick input types that match the data, add datalist and autocomplete hints, read the form with FormData, and remember that disabled fields are never submitted.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course