JavaScript
x
8
1
<html>
2
3
<body>
4
5
// element should be inserted here
6
</body>
7
</html>
8
I’m not very familiar with vanilla Javascript, always have worked with jQuery. I tried this so far, but that got the element in the middle of <head>
and <body>
.
JavaScript
1
3
1
var bodyTag = document.getElementsByTagName('body')[0];
2
bodyTag.parentNode.insertBefore(myElement, bodyTag);
3
Advertisement
Answer
It’s pretty simple. Using appendChild method it can be written as short as:
JavaScript
1
2
1
document.body.appendChild(myElement);
2