Javascript Form How to - Create input Elements








Question

We would like to know how to create input Elements.

Answer


<!DOCTYPE html>
<html>
<head>
</head><!--  w  w  w. ja  va  2  s.  c  o m-->
<body>
  <input type="button" id="btn" value="Add" onclick="newFunc2()">
  <div id="new1"></div>
  <script type='text/javascript'>
function newFunc2(){
    a=document.createElement('div');
    b = document.createElement('input'); 
    a.appendChild(b);
    c=document.createElement('div');
    c.appendChild(a);
    var d=document.getElementById('new1');
    d.appendChild(c);
}

</script>
</body>
</html>

The code above is rendered as follows: