Javascript Form How to - Add Values to a DOM element after the element has been created








Question

We would like to know how to add Values to a DOM element after the element has been created.

Answer


<!DOCTYPE html>
<html>
<body>
  <script type='text/javascript'>
var elem = document.createElement("input");
elem.type = "text";
elem.id = "test";
document.body.appendChild(elem);<!--from   w  w  w  .  j  a va2 s. c om-->
elem.value = "This is some text";

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

The code above is rendered as follows: