1

My AJAX request responds with the following data in string format:

<tr> <td>data</td> <td>data</td> <td>data</td> </tr>
<tr> <td>data</td> <td>data</td> <td>data</td> </tr>
<tr> <td>data</td> <td>data</td> <td>data</td> </tr>
<tr> <td>data</td> <td>data</td> <td>data</td> </tr>

I want to append this HTML to an existing table.

<table id="demo">
  <tr> <td>data</td> <td>data</td> <td>data</td> </tr>
  <tr> <td>data</td> <td>data</td> <td>data</td> </tr>
  <tr> <td>data</td> <td>data</td> <td>data</td> </tr>
</table>

I have already selected the table element from the DOM.

const table = document.getElementById('demo');

I would like to be able to do something like the following, but of course that is not possible using the appendChild method.

table.appendChild(xhr.responseText);

Is what I'm working to accomplish possible using only the DOM (without the use of libraries), and if so how can I go about using it properly?

0

3 Answers 3

2

This is exactly what Element#insertAdjacentHTML was designed for. This prevents the waste that the use of Element#innerHTML causes when it sends all of the existing HTML through the parser again, which can be costly with large tables and in turn removes any event listeners that may be bound to the existing elements and references to the existing DOM nodes that will be destroyed.

It is generally always safer to use this method over the Element#innerHTML.

Here is a simple example that achieves the result you requested in your question:

const rows = `
  <tr> <td>data</td> <td>data</td> <td>data</td> </tr>
  <tr> <td>data</td> <td>data</td> <td>data</td> </tr>
  <tr> <td>data</td> <td>data</td> <td>data</td> </tr>
  <tr> <td>data</td> <td>data</td> <td>data</td> </tr>
`;

const table = document.getElementById('demo');
table.insertAdjacentHTML('beforeend', rows);
<table id="demo">
  <tr> <td>data</td> <td>data</td> <td>data</td> </tr>
  <tr> <td>data</td> <td>data</td> <td>data</td> </tr>
  <tr> <td>data</td> <td>data</td> <td>data</td> </tr>
  <tr> <td colspan="3"><hr></td> </tr>
</table>

Here is an example that shows the behavior of each position:

const table = document.getElementById('demo');

const rows = data => `
  <tr> <td>${data}</td> <td>${data}</td> <td>${data}</td> </tr>
  <tr> <td>${data}</td> <td>${data}</td> <td>${data}</td> </tr>
`;
/** <= ECMAScript 2015 
 * function rows(data) { 
 *   return [ '<tr> <td>','</td> <td>','</td> <td>','</td> </tr>' +
 *            '<tr> <td>','</td> <td>','</td> <td>','</td> </tr>' ].join(data);
 * }
 **/

const insert = position => table.insertAdjacentHTML(position, rows(position));
/** <= ECMAScript 2015 
 * function insert(position) { 
 *   return table.insertAdjacentHTML(position, rows(position));
 * }
 **/

const positions = ['beforebegin', 'afterbegin', 'beforeend', 'afterend'];

positions.forEach(insert);
<table id="demo">
  <tr> <td>existing data</td> <td>existing data</td> <td>existing data</td> </tr>
  <tr> <td>existing data</td> <td>existing data</td> <td>existing data</td> </tr>
</table>

Sign up to request clarification or add additional context in comments.

Comments

1

You can just add it to the end of the innerHTML.

table.innerHTML += xhr.responseText;

That will basically add xhr.responseText to the end of the HTML within the element.

Comments

0

You can try this:

var ajaxData = `<tr> <td>data1-1</td><td>data1-2</td> </tr>
<tr> <td>data2-1</td><td>data2-2</td> </tr>
<tr> <td>data3-1</td><td>data3-2</td> </tr>
<tr> <td>data4-1</td><td>data4-2</td> </tr>`;

var table = document.getElementById('thatTable');

var template = document.createElement('template');
template.innerHTML = ajaxData;
table.appendChild(template.content);
#thatTable td {
  border: 1px solid black;
}
<table id="thatTable">
  <tr>
    <th>Head 1</th>
    <th>Head 2</th>
  </tr>
</table>

I use a template to hold the HTML that is returned by your ajax call and then append the content of the template into your table.

I do it this way in case you already have things in the table, like in my example.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.