The script runs before the document
loaded, so why does console.log(b)
show elements in the collection?
If console.log(b)
has elements then why does console.log(b[0])
show undefined
and console.log(b.length)
0
?
<html>
<head>
<script>
function test(){
var b = document.getElementsByName('a');
console.log(b);
console.log(b[0]);
console.log(b.length);
}
test();
</script>
</head>
<body>
<form id="a" name="a"></form>
</body>
</html>