Javascript DOM HTML document forms Collection get by items(index)

Introduction

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

var x = document.forms.item(0).id;

Click the button to display the id of the first form element (index 0) in the document.

View in separate window

<!DOCTYPE html>
<html>
<body>

<form id="myCarForm">
  Favorite Car: <input type="text" name="fname" value="Javascript">
</form>//ww w  .  ja  v a  2 s. com

<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.item(0).id;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>



PreviousNext

Related