jQuery append with `.load()`without using `.get()`

1.7k Views Asked by At

It appears that one (of many) differences between .load() and .get() is that the former can use an HTML fragment, while the latter requires that said HTML fragment be a fully valid XML structure.

I'm trying to load an external file that is a fragment of HTML and append it to an element. However load, by default, replaces--not appends.

It seems the common solution to this is to use .get() instead, but that's not going to work in my situation as the file is likely rarely to be a valid xml document.

Is there a way to use .load() for this?

And example of the HTML I'm working with:

<table>
    <tr><td>Hello</td></tr>
</table>

and the html file:

<tr><td>World</td></tr>

Can I use .load() to append the TR in the external file into the TABLE?

1

There are 1 best solutions below

1
On BEST ANSWER

Also using get() should be better you might use load:

var $tr = $("<tr/>").load('ur/file.html',function(){
  $tr.appendTo("#myTable")
})

Get

$.get( url, function(data){
  $(data).appendTo("#myTable")
}, 'text' );