Javascript DOM HTML document forms Collection get by namedItem(id)

Introduction

Get the HTML content of the <form> element with id="myCarForm" in the document:

var x = document.forms.namedItem("myCarForm").innerHTML;

Click the button to display the HTML content of the form element with id="myForm".

View in separate window

<!DOCTYPE html>
<html>
<body>

<form id="myCarForm">
  Favorite Car: <input type="text" name="fname" value="Javascript">
</form>/*from   w  w  w  . ja  v a2s  . c om*/

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

</body>
</html>



PreviousNext

Related