Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

193
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda