Javascript DOM HTML Form Object create

Introduction

We can create a <form> element via the document.createElement() method:

var x = document.createElement("FORM");

Click the button to create a FORM and an INPUT element.

View in separate window

<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Test</button>

<script>
function myFunction() {/* ww w  .j  a v  a 2  s.c  o m*/
  var x = document.createElement("FORM");
  x.setAttribute("id", "myForm");
  document.body.appendChild(x);

  var y = document.createElement("INPUT");
  y.setAttribute("type", "text");
  y.setAttribute("value", "Set from Javascript");
  document.getElementById("myForm").appendChild(y);
}
</script>

</body>
</html>



PreviousNext

Related