Document forms Collection - Using the elements collection together with document.forms to get the value of each element in the form: - Javascript DOM

Javascript examples for DOM:Document forms

Description

Document forms Collection - Using the elements collection together with document.forms to get the value of each element in the form:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

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

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

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

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

</body>
</html>

Related Tutorials