HTML DOM form collection

WBOY
Release: 2023-09-01 14:49:01
forward
565 people have browsed it

HTML DOM form collection is used to return all form elements present in the HTML document as a collection. The elements in the collection are sorted and displayed in the order in which they appear in the HTML document.

Properties

The following are the properties of the form collection-

PropertiesDescription
lengthIt is a read-only property that returns the number of
elements in the collection.

Method

The following is the method of form collection-

MethodDescription
[index]Returns the
element from the collection at the given index. Indexes start at 0 and return null if the index is out of bounds.
item(index)The collection from the given index. The index starts at 0 and returns null if the index is out of range.
namedItem(id)From the collection with the given id. If id does not exist, returns Null.

Syntax

The following is the syntax for HTML DOM form collection - p>

document.forms
Copy after login

Example

Let’s take a look at the HTML Example of DOM Form Collection -

Live Demo






Forms collection example

Fruit
Age
Password:

Following are the form ids

Copy after login

Output

This will produce the following output-

HTML DOM表单集合

Single When clicking the "Get IDS" button-

HTML DOM表单集合

In the above example-

we first created three IDs as "FORM1" and "FORM2" form and "FORM3". The first two forms have an input element of text type and the third form has an input element of password type -

Fruit
Age
Password:
Copy after login

GET IDS button executes formCollect() method when user clicks-

Copy after login

The formCollect() method gets the document.forms length property value (3 in our case) and uses it in the test expression inside the for loop. Using the index number on the form collection, we get their id and append it to the paragraph with id "Sample" for display -

function formCollect() {
   for(var i=0;i";
      document.getElementById("Sample").innerHTML +=no;
   }
}
Copy after login

The above is the detailed content of HTML DOM form collection. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!