Javascript DOM HTML document forms Collection get by index

Introduction

Get the id of the first <form> element at index 0 in the document:

var x = document.forms[0].id;

View in separate window

<!DOCTYPE html>
<html>
<body>

<form id="myCarForm">
  Favorite Car: <input type="text" name="fname" value="Javascript">
</form>/*  w ww  . j a  v a2 s.  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>



PreviousNext

Related