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

198
Vistas
Why doesn't `appendChild` work as expected when passed an anonymous function?

I'm trying to pass an anonymous function to the appendchild function.

I'm getting the following error message though:

Uncaught TypeError: Node.appendChild: Argument 1 does not implement interface Node.

Seems as if the anonymous function isn't returning the required type? In comparison, if I define a named function with the same code in it and pass that to the appendChild function I'm not getting an error.

See the following code for clarification:

// Option 1
function appendThis() {
  var parent = document.getElementById("parent");
  parent.appendChild(function () {
    var child = document.createElement("div");
    child.classList.add("child");
    child.classList.add("red");
    child.innerHTML = "appendThis()";
    return child;
  });
}

// Option 2
function appendThat() {
  var parent = document.getElementById("parent");
  var child = document.createElement("div");
  child.classList.add("child");
  child.classList.add("green");
  child.innerHTML = "appendThat()";
  parent.appendChild(child);
}

// Option 3
function createChild() {
  var child = document.createElement("div");
  child.classList.add("child");
  child.classList.add("yellow");
  child.innerHTML = "createChild()/appendThese()";
  return child;
}
function appendThese() {
  var parent = document.getElementById("parent");
  parent.appendChild(createChild());
}
main{
  height: 98vh;
  width: 98vw;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
button{
  height: 50px;
  width: 200px;
}
hr{
  width: 200px;
}

.parent {
  height: 100%;
  width: 100%;
}
.child{
  height: 30px;
  width: 200px;
  text-align: center;
}
.red {
  background-color: red;
}
.green {
  background-color: green;
}
.yellow {
  background-color: yellow;
}
<body>
    <main>
        <div id="parent"></div>
        <hr>
        <button class="red" onclick="appendThis();">AppendThis</button>
        <button class="green" onclick="appendThat();">AppendThat</button>
        <button class="yellow" onclick="appendThese();">AppendThese</button>
    </main>
</body>

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

parent.appendChild(function () {
  var child = document.createElement("div");
  child.classList.add("child");
  child.classList.add("red");
  child.innerHTML = "appendThis()";
  return child;
});

You did not actually execute your function there.

You need to add () after the function definition, if you want to execute it at this point.

This is what's called an IIFE - Immediately-Invoked Function Expression. More details on that can be found here: What is the (function() { } )() construct in JavaScript?

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