Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

HTML DOM Form elements collection

Form elements collection

Form Object Reference Form Object


Example

Get the number of elements in the form:

var

x

=

document

.

getElementById

(

"

myForm

"

)

.

elements

.

length

;

Try it »

Definition and Usage

The elements collection returns all elements of the form as an array.

**Note:** The order of elements in the collection is arranged by their position in the source code.

**Note:** The elements collection returns all

elements in the form, not all elements in the HTML document. To get all elements you can use document.forms collections.

Syntax

formObject.elements

Browser support

Union
elements Yes Yes Yes Yes Yes

property

property Description
length Returns the number of all elements in the form.
 **注意:** 这个属性是只读的。 |

Method

Method Description
[index] Returns the element at the specified index (starting from 0) in the form.
 **注意:** 如果索引值超过最大索(或者说超过指定区间)引值返回 null 。 |

| item(index) | Returns the element at the specified index (starting from 0) in the form.

 **注意:** 如果索引值超过最大索(或者说超过指定区间)引值返回 null 。 |

| namedItem(id) | Returns the element with the specified id in the form.

 **注意:** 如果 id 不存在 返回 null。 |

Example

Example

[index]

Get the value of the first element (index 0) in the form:

var

x

=

document

.

getElementById

(

"

myForm

"

)

.

elements

[

0

]

.

value

;

Try it »

Example

item(index)

Get the value of the first element (index 0) in the form:

var

x

=

document

.

getElementById

(

"

myForm

"

)

.

elements

.

item

(

0

)

.

value

;

Try it »

Example

Loop through and output the value of each element in the form:

var

x

=

document

.

getElementById

(

"

myForm

"

)

;

var

txt

=

"

"

;

var

i

;

for

(

i

=

0

;

i

<

x

.

length

;

i

++

)

{

txt

=

txt

x

.

elements

[

i

]

.

value

"


"

;

}

document

.

getElementById

(

"

demo

"

)

.

innerHTML

=

txt

;

Try it »


Form Object Reference Form Object