Document forms Collection [index] - Get the id of the first <form> element (index 0) in the document: - Javascript DOM

Javascript examples for DOM:Document forms

Description

Document forms Collection [index] - Get the id of the first <form> element (index 0) in the document:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<form id="myCarForm">
  Favorite Car: <input type="text" name="fname" value="Volvo">
</form>//  w w w.j  ava 2s  . co m

<form id="myColorForm">
  Favorite Color: <input type="text" name="favcolor" value="Blue">
</form>

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

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

<script>
function myFunction() {
    var x = document.forms[0].id;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

Related Tutorials