Exercise 7: Get All Form Values as Object

Problem Statement

Function `getFormData(formEl)` banao jo form ke sabhi input values ko object me return kare. Example: getFormData(form) => {name: "John", email: "john@example.com"}

Sample Output:

getFormData(formEl) => {name: "John", email: "john@example.com"}
// Returns object with all form field values

Solution

const getFormData = (formEl) => {
  const data = {};
  Array.from(formEl.elements).forEach(el => {
    if (el.name && el.value) data[el.name] = el.value;
  });
  return data;
};

Explanation

Overall Goal:

  • Form ke sabhi values ko object me collect karna.

Real world:

  • Form submission: data collection.
  • Form serialization: data extraction.