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');
}
});
CodePudding user response:
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);