JavaScript DOM Form Object

1. Form Object

  • Form object represents an HTML form.
  • It is used to collect user input through elements like text fields, check box and radio button, select option, text area, submit buttons and etc.
Syntax:
<form> . . . </form>

Form Object Properties

PropertyDescription
ActionIt sets and returns the value of the action attribute in a form.
enctypeIt sets and returns the value of the enctype attribute in a form.
LengthIt returns the number of elements in a form.
MethodIt sets and returns the value of the method attribute in a form that is GET or POST.
NameIt sets and returns the value of the name attribute in a form.
TargetIt sets and returns the value of the target attribute in a form.

Form Object Methods

MethodDescription
reset()It resets a form.
submit()It submits a form.

2. Hidden Object

  • Hidden object represents a hidden input field in an HTML form and it is invisible to the user.
  • This object can be placed anywhere on the web page.
  • It is used to send hidden form of data to a server.
Syntax:
<input type= “hidden”>

Hidden Object Properties

PropertyDescription
NameIt sets and returns the value of the name attribute of the hidden input field.
TypeIt returns type of a form element.
ValueIt sets or returns the value of the value attribute of the hidden input field.

3. Password Object

  • Password object represents a single-line password field in an HTML form.
  • The content of a password field will be masked – appears as spots or asterisks in the browser using password object.
Syntax:
<input type= “password”>

Password Object Properties

PropertyDescription
defaultValueIt sets or returns the default value of a password field.
maxLengthIt sets or returns the maximum number of characters allowed in a password filed.
NameIt sets or returns the value of the name attribute of a password field.
readOnlyIt sets or returns whether a password fields is read only or not.
SizeIt sets or returns the width of a password field.
ValueIt sets or returns the value of the attribute of the password field.

Password Object Methods

MethodDescription
select()It selects the content of a password field.

4. Checkbox Object

  • Check box object represents a checkbox in an HTML form.
  • It allows the user to select one or more options from the available choices.
Syntax:
<input type= “checkbox”>

Checkbox Object Properties

PropertyDescription
NameIt sets or returns the name of the checkbox.
TypeIt returns the value “check”.
ValueIt sets or returns the value of the attribute of a checkbox.
checkedIt sets or returns the checked state of a checkbox.
defaultCheckedIt returns the default value of the checked attribute.

Checkbox Object Methods

MethodDescription
click()It sets the checked property.

5. Select Object

  • Select object represents a dropdown list in an HTML form.
  • It allows the user to select one or more options from the available choices.
Syntax:
<select> … </select>

Select Object Collections

CollectionDescription
optionsIt returns a collection of all the options in a dropdown list.

Select Object Properties

PropertyDescription
LengthIt returns the number of options in a dropdown list.
selectedIndexIt sets or returns the index of the selected option in a dropdown list.
TypeIt returns a type of form element.
nameIt returns the name of the selection list.

Select Object Methods

MethodDescription
add()It adds an option to a dropdown list.
remove()It removes an option from a dropdown list.

6. Option Object

  • Option object represents an HTML <option> element.
  • It is used to add items to a select element.
Syntax:
<option value> . . . </option>

Option Object Properties

PropertyDescription
IndexIt sets or returns the index position of an option in a dropdown list.
TextIt sets or returns the text of an option element.
defaultSelectedIt determines whether the option is selected by default.
ValueIt sets or returns the value to the server if the option was selected.
PrototypeIt is used to create additional properties.

Option Object Methods

MethodsDescription
blur()It removes the focus from the option.
focus()It gives the focus to the option.

Example : Simple Program on Option Object Method

<html>
<head>
     <script type="text/javascript">
     function optionfruit(select)
     {
          var a = select.selectedIndex;
          var fav = select.options[a].value;
          if(a==0)
          {
               alert("Please select a fruit");
          }
          else
          {
               document.write("Your Favorite Fruit is <b>"+fav+".</b>");
          }
     }
     </script>
</head>
<body>
     <form>
          List of Fruits:
          <select name="fruit">
               <option value="0">Select a Fruit</option>
               <option value="Mango">Mango</option>
               <option value="Apple">Apple</option>
               <option value="Banana">Banana</option>
               <option value="Strawberry">Strawberry</option>
               <option value="Orange">Orange</option>
          </select>
          <input type="button" value="Select" onClick="optionfruit(this.form.fruit);">
     </form>
</body>
</html>


Output:

option object1
Your Favorite Fruit is Mango.

7. Radio Object

Radio object represents a radio button in an HTML form.

Syntax:
<input type= “radio”>

Radio Object Properties

PropertyDescription
CheckedIt sets or returns the checked state of a radio button.
defaultCheckedReturns the default value of the checked attribute.
NameIt sets or returns the value of the name attribute of a radio button.
TypeIt returns the type of element which is radio button.
ValueIt sets or returns the value of the radio button.

Radio Object Methods

MethodDescription
blur()It takes the focus away from the radio button.
click()It acts as if the user clicked the button.
focus()It gives the focus to the radio button.

8. Text Object

Text object represents a single-line text input field in an HTML form.

Syntax:
<input type= “text”>

Text Object Properties

PropertyDescription
ValueIt sets or returns the value of the text field.
defaultValueIt sets or returns the default value of a text field.
NameIt sets or returns the value of the name attribute of a text field.
maxLengthIt sets or returns the maximum number of characters allowed in a text field.
readOnlyIt sets or returns whether a text field is read-only or not.
SizeIt sets or returns the width of a text field.
TypeIt returns type of form element of a text field.

9. Textarea Object

Textarea object represents a text-area in an HTML form.

Syntax:
<textarea> . . . </textarea>

Textarea Object Properties

PropertyDescription
ValueIt sets or returns the value of the text field.
defaultValueIt sets or returns the default value of a text field.
NameIt sets or returns the value of the name attribute of a text field.
TypeIt returns type of form element of a text field.
RowsIt displays the number of rows in a text area.
ColsIt displays the number of columns in a text area.