Skip to content
Home ยป Forms Used by a Web Site

Forms Used by a Web Site

Here is a detailed, MCA-level explanation (website-ready) of

๐ŸŒ The Form Object in JavaScript

and its Elements & Methods (with examples)


๐Ÿง  1. The Form Object

In JavaScript, a Form Object represents an HTML <form> and provides access to its elements, properties, and methods.

๐Ÿ‘‰ It is part of the Document Object Model (DOM).

๐Ÿ”น Accessing a Form

<form name="myForm">
  <input type="text" name="username">
</form>

<script>
  let form = document.forms["myForm"];
  console.log(form);
</script>

๐Ÿ“Œ Key Properties:

  • document.forms โ†’ collection of all forms
  • form.elements โ†’ access all elements inside form

โš™๏ธ 2. Form Object Methods

MethodDescription
submit()Submits the form
reset()Resets all fields
focus()Focus on input field
blur()Removes focus

๐Ÿงฉ 3. Form Elements with Examples


โœ๏ธ (1) Text Element

Used to input single-line text.

<input type="text" id="name">

<script>
  let val = document.getElementById("name").value;
  console.log(val);
</script>

๐Ÿ”’ (2) Password Element

Used to input hidden text (password).

<input type="password" id="pass">

<script>
  let pwd = document.getElementById("pass").value;
</script>

๐Ÿ”˜ (3) Button Element

Triggers JavaScript functions.

<button onclick="show()">Click</button>

<script>
function show() {
  alert("Button Clicked");
}
</script>

๐Ÿ“ค (4) Submit Button

Submits form data to server.

<form onsubmit="return validate()">
  <input type="text" id="user">
  <input type="submit" value="Submit">
</form>

<script>
function validate() {
  let u = document.getElementById("user").value;
  if(u === "") {
    alert("Enter name");
    return false;
  }
}
</script>

๐Ÿ”„ (5) Reset Button

Resets all form fields.

<form>
  <input type="text" id="name">
  <input type="reset" value="Reset">
</form>

โ˜‘๏ธ (6) Checkbox Element

Allows multiple selections.

<input type="checkbox" id="chk"> Accept Terms

<script>
  let status = document.getElementById("chk").checked;
</script>

๐Ÿ”˜ (7) Radio Element

Allows single selection from a group.

<input type="radio" name="gender" value="male"> Male
<input type="radio" name="gender" value="female"> Female
let radios = document.getElementsByName("gender");
for(let r of radios) {
  if(r.checked) {
    console.log(r.value);
  }
}

๐Ÿ“ (8) TextArea Element

Used for multi-line input.

<textarea id="msg"></textarea>

<script>
  let message = document.getElementById("msg").value;
</script>

๐Ÿ“‹ (9) Select and Option Element

Used to create dropdown lists.

<select id="city">
  <option value="Delhi">Delhi</option>
  <option value="Mumbai">Mumbai</option>
</select>

<script>
  let city = document.getElementById("city").value;
</script>

๐Ÿ”ข (10) Multi-Choice Select List

Allows multiple selections.

<select id="courses" multiple>
  <option value="Java">Java</option>
  <option value="Python">Python</option>
  <option value="JS">JavaScript</option>
</select>
let list = document.getElementById("courses");
for(let opt of list.options) {
  if(opt.selected) {
    console.log(opt.value);
  }
}

๐Ÿ”„ Complete Example (All Elements Together)

<form id="myForm">

  Name: <input type="text" id="name"><br><br>

  Password: <input type="password" id="pass"><br><br>

  Gender:
  <input type="radio" name="gender" value="Male"> Male
  <input type="radio" name="gender" value="Female"> Female<br><br>

  Hobbies:
  <input type="checkbox" id="sports"> Sports
  <input type="checkbox" id="music"> Music<br><br>

  Message:<br>
  <textarea id="msg"></textarea><br><br>

  City:
  <select id="city">
    <option>Delhi</option>
    <option>Mumbai</option>
  </select><br><br>

  Courses:
  <select id="courses" multiple>
    <option>Java</option>
    <option>Python</option>
    <option>JS</option>
  </select><br><br>

  <button type="button" onclick="getData()">Show Data</button>
  <input type="reset">

</form>

<script>
function getData() {
  let name = document.getElementById("name").value;
  let city = document.getElementById("city").value;
  alert(name + " - " + city);
}
</script>

๐Ÿ”„ Summary Table

ElementPurpose
TextSingle-line input
PasswordHidden input
ButtonTrigger action
SubmitSend form
ResetClear form
CheckboxMultiple choice
RadioSingle choice
TextAreaMulti-line input
SelectDropdown
Multi SelectMultiple dropdown choices

๐ŸŽฏ Conclusion

The Form Object and its elements are essential for:

  • Collecting user input
  • Validating data
  • Building interactive web applications

๐Ÿ‘‰ Mastering forms is crucial for real-world web development and MCA exams.