1

JavaScript Element.length returns invalid length while executing in Visualforce page.

I'm expecting body NodeList length to be 14. But after printing console.log('allDocElements length:: ',allDocElements.childNodes.length); It return 10. how this is possible?

I'm executing it in Google Chrome.

View console output

<apex:page showHeader="false" sidebar="false">
<html>
    <head>
        <title>JS Addignment 1</title>
        <style>
            body {
                text-align:center;
                font-size:30px;
            }
            .center {
                width:70%; 
                margin:15% auto;
                border-collapse:collapse;
                border:2px solid #000000;
            }
            table td {
                border:2px solid #000000;
            }
        </style>
        <script>
        let dateContainer, dayContainer, timeContainer;
        let allDocElements = document.getElementsByTagName('body')[0];            

        console.log('allDocElements::',allDocElements.childNodes);



        </script>
    </head>
    <body>
        <table border="1" style="" class="center" cellpadding="20">
            <tbody>
                <tr>
                    <td><label>Date: </label><span id="dateContainer"/></td>
                    <td><label>Day: </label><span id="dayContainer"/></td>
                </tr>
                <tr>
                    <td colspan="2"><span id="timeContainer"/></td>
                </tr>
            </tbody>
        </table>
    </body>
</html>

Amit Aher
  • 11
  • 2
  • Possible duplicate of [Is Chrome's JavaScript console lazy about evaluating arrays?](https://stackoverflow.com/questions/4057440/is-chromes-javascript-console-lazy-about-evaluating-arrays) – VLAZ Oct 02 '19 at 07:32
  • 1
    The count *was* 10 when you printed this to the console. More nodes were added afterwards. The console only evaluates objects *later* when you expand it, so you don't get a snapshot of the elements at the time you print them. – VLAZ Oct 02 '19 at 07:33
  • Thanks, @VLAZ for your 2nd solution. I got the issue resolved after I have put the – Amit Aher Oct 02 '19 at 07:47

0 Answers0