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

280
Views
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 answers
Answer question

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 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!