Form length Property - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Form

Description

The length property returns the number of elements in a form.

Return Value

A Number, representing the number of elements in the form

The following code shows how to return the number of elements in a form:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<form id="myForm" action="#">
  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 w  ww  .j  a v a 2 s.  co  m*/

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

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

<script>
function myFunction() {
    var x = document.getElementById("myForm");
    var txt = "";
    var i = 0;
    for (i = 0; i< x.length; i++) {
        txt = txt + x.elements[i].value + "<br>";
    }
    document.getElementById("demo").innerHTML = txt;
}
</script>

</body>
</html>

Related Tutorials