I am appending some html to a div using jQuery:
public/js/writeInputs.js
$("#myDiv").append("<input type='search' value='<%= filters.title %>' name='filters[title]' class='form-control'>")
In this case filters is an object that I'm passing from my node express backend, the value of which I want to display in my input. However, the above renders the ejs variable as the string literal:
<%= filters.title %>
If I hardcode this value into the html it works as expected:
views/partials/inputs.ejs
<input type="search" name="filters[title]" value="<%= filters.title %>" class="form-control">
How can I write an ejs variable in jQuery?
I've tried these approaches in my append, but none work:
value="<%= filters.title %>"
value=${<%= filters.title %>}
value='"<%= filters.title %>"'
appendhappens in ajsfile, but the element that it appears on is anejstemplatewriteInputs.jsdoes not get served as static asset from your public dir but instead has its own GET routing function so that your server can load in the file and respond usingrespond.render(...)?ejssyntax. Any file with ejs syntax needs to be served that way, they cannot just be put in the static/public dir and loaded directly from that file by the user. The URL request forwriteInputs.jsneeds to go to an actual serving route (likeapp.get('writeInputs.js', ...)if you're using express) and you have to use ejs rendering if you want your ejs syntax to get replaced.