Accessing Form Fields in JavaScript

Accessing Form

JavaScript provides various ways of accessing form:-

  • document.forms[index_number] – It returns collection of form in the documents.
  • document.forms[“name_attribute_value”] – we can use name attribute of the form.
  • document.name_attribute_value – we can use name attribute of the form with dot as well.
  • getElementById(“ID”) – If form as an unique id we can use it.

Accessing Form Fields

If form fields have unique ID then it is possible to access them using the getElementById( ) method. However, There are some other ways: –

  • elements[ ] – It contains collection of form fields.

Syntax: –

document.form_name.elements[index_number];
document.form_name.elements[“name_attribute_value”];
document.form_name.name_attribute_value;
document.form_name.ID_attribute_value;

  • getElementsByName(“field_name”) – This method is also used to access form field.
Tagged : / / /