user2829448 user2829448 - 7 months ago 84
Javascript Question

Appending EJS partial with jQuery

I'm trying to use JQuery to append an EJS partial. What I want to do is to implement infinite scrolling in a table - I am using EJS to render the table's rows as partials and within each partial using more EJS to display the variables passed into the view from Express.

table.html

<tbody>
<% include row.html %>
</tbody>


row.html

<% users.forEach(function(user){ %>
<td><%= user.name %></td>
...and so on
<% }) %>


jQuery

$('tbody').append('<%- include row.html %>')


The issue I'm having now is that I can't get the last <% include row.html %> to work in the jQuery. I have debugged this and isolated the issue to this line of code. What can I do to make this work?

Thanks!

Answer

Make sure to escape your HTML, meaning if it contains a single quote, it won't work.

$('tbody').append('<p>Johny's HTML AIN'T werkin'!</p>');

should be

$('tbody').append('<p>Johny\'s HTML IS werkin\'!</p>');