Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

192
Views
Uncaught TypeError: Cannot read properties of null (reading 'children') at window.onload

I am trying to get the children of a div, however, for some reason, it won't work.

I get the div via the getElementById method and then when I try to get the children of that element, it won't recognize it and throws me an error

window.onload = function(){
    console.log("h")
    for (i in bookData ){
        var current = bookData[i]
        var currentCodeN = current.codeName
        var currentHTMLElem = document.getElementById(toString(currentCodeN))
        console.log(typeof(currentHTMLElem)) //returns 'object'
        console.log(typeof(currentHTMLElem.children)); //error
        console.log(currentHTMLElem.children.length);
    }
}

Any idea why this is happening? Searched for this issue and set up an event to wait for the whole page to be loaded. Still doesn't work

Error:

Uncaught TypeError: Cannot read properties of null (reading 'children')
    at window.onload"

I am not using iFrame

UPDATE: I was using toString on an already string. Sorry.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

There are a few things going on here.

var currentHTMLElem = document.getElementById(toString(currentCodeN))

This is making currentHTMLElem null. Will get back to why, but it is clearly null because the error message tells you it is.

console.log(typeof(currentHTMLElem)) //returns 'object'

Yes, if currentHTMLElem is null, typeof will return "object". So there you are.

I don't know what bookData is, but I'm sure using toString is incorrect. Look at what toString does for various objects:

toString(null) -> "[object Undefined]"

toString(12) -> "[object Undefined]"

toString(anythingatall) -> "[object Undefined]"

That's because you are calling the wrong toString. Calling toString without an object will use the base Object.prototype.toString with an undefined object. If you invoke it with an object like toString.apply(12) you will get the base version which just tells you the type ([Object Number]). Instead, you would want to call the overloaded version: currentCodeN.toString(), which will call to toString for whatever type currentCodeN is. For a string, this just returns the string, for a Number it will return a string version.

However, that will raise an error is currentCodeN is null or undefined. You shouldn't need to use toString at all. I'm guessing it is already a string, so try just:

var currentHTMLElem = document.getElementById(current.codeName)

or for maximum safety:

var currentHTMLElem = document.getElementById(current && current.codeName || '')

if that still gives you null, then the element doesn't exist in the DOM.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!