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

287
Vistas
How to use insertBefore and appendChild with a jQuery AJAX server side rendered html result

I basically need to use insertBefore and appendChild with a Server Side rendered HTML page jQuery AJAX result. I get the error: "Failed to execute 'inserBefore' on 'Node': parameter 1 is not of type 'Node'."

var parent_elem = $('#parent_elem');
$.ajax({
    type:'POST',
    url:'example.com/my_ajax_call',
    data: {"my_data":my_data},
    success:function(data){
        parent_elem.prepend(data); //THIS IS THE JQUERY SOLUTION BUT I NEED PURE JS WHILE USING JQUERY AJAX REQUEST
    },
    fail:function(data){
        console.log(data);
        console.log('failed to load page');
    }
});

what I am trying to do is this:

var parent_elem = document.getElementById('parent_elem');
$.ajax({
    type:'POST',
    url:'example.com/my_ajax_call',
    data: {"my_data":my_data},
    success:function(data){
        parent_elem.inserBefore(data, parent_elem.firstChild); //THIS IS WHAT I NEED BUT IT THROWS ERROR "Failed to execute 'inserBefore' on 'Node': parameter 1 is not of type 'Node'."
    },
    fail:function(data){
        console.log(data);
        console.log('failed to load page');
    }
});
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

If you want to turn HTML string into a node you can use jQuery function around it:

var node = $("<h1>hello world</h1>")

So in your case:

parent_elem.inserBefore($(data)[0], parent_elem.firstChild); 
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