html - createElement and appendChild in JavaScript -
i trying figure out why createelement , appendchild aren't working. believe i've got code correct i'm not sure why it's not working... (i using ie 10)
code updated
javascript file:
var myobj = { firstname: "john", lastname: "smith" };
html file:
<!doctype html /> <html> <title>the data structure</title> <body> <script type="text/javascript" src="thedata.js"> var theelement = document.createelement('p'); var thetext = document.createtextnode(myobj.firstname); theelement.appendchild(thetext); document.body.appendchild(theelement); </script> </body> </html>
the issue both supply src
on script
tag and provide content within it. that's invalid, can 1 or other, not both. browsers use src
, disregard content.
separate that, creating element doesn't put in dom. have add somewhere (via appendchild
or insertbefore
or similar).
so perhaps:
<script src="thedata.js"></script> <!-- note: 2 separate script elements --> <script> var theelement = document.createelement('p'); var thetext = document.createtextnode(myobj.firstname); theelement.appendchild(thetext); document.body.appendchild(theelement); // <==== note: adding `the element` dom </script>
Comments
Post a Comment