Create a Form Element - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Form

Introduction

You can create a <form> element by using the document.createElement() method:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">create a FORM and an INPUT element</button>

<script>
function myFunction() {//from   w  w w  .j av a  2s. co  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", "Donald");
    document.getElementById("myForm").appendChild(y);
}
</script>

</body>
</html>

Related Tutorials