Form elements Collection - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Form

Description

The elements collection returns all elements in a form sorted as they appear in the source code.

Syntax

Properties

Property Description
length number of elements in the <form> element.

Methods

MethodDescription
[index] element in <form> with the specified index (starts at 0).
item(index) element in <form> with the specified index (starts at 0).
namedItem(id) element in <form> with the specified id.

Technical Details

Return Value:

An HTMLFormsControlCollection Object, representing all elements in a <form> element.

The following code shows how to Find out how many elements there are in a specified <form> element:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<form id="myForm" action="/action_page.php">
  First name: <input type="text" name="fname" value="Donald"><br>
  Last name: <input type="text" name="lname" value="Duck"><br>
  <input type="submit" value="Submit">
</form>//from ww  w . ja va 2 s.c om

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {
    var x = document.getElementById("myForm").elements.length;
    document.getElementById("demo").innerHTML = "Found " + x + " elements in the form.";
}
</script>

</body>
</html>

Related Tutorials