Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

196
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda