I want to get the length or item of body tag. But, It seems getElementsByTagName doesn't work in terms of NodeList. How can I do that?
<script type=text/javascript>
var b = document.getElementsByTagName('body');
console.log(b); // <body>...</body>
console.log(b.length); // 0
console.log(b[0]); // undefined
console.log(b.item(0)); // null
</script>
Thanks.
[Edit] I added entire body.
<html>
<head>
<script type=text/javascript>
var b = document.getElementsByTagName('body');
console.log(b); // <body>...</body>
console.log(b.length); // 0
console.log(b[0]); // undefined
console.log(b.item(0)); // null
</script>
</head>
<body>
<div id='test'>
<h2>Hello</h2>
<p>test</p>
<p>test</p>
<p>test</p>
<p>test</p>
<p>test</p>
<p>test</p>
</body>
</html>
Place the script at the end of the page, or place it in a function and call it after the entire page has loaded.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With