4

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 %>"'
6
  • Your code is in a ejs file or a regular js file? Commented Jun 11, 2019 at 18:02
  • The append happens in a js file, but the element that it appears on is an ejs template Commented Jun 11, 2019 at 18:49
  • 1
    did you remember to make sure that writeInputs.js does 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 using respond.render(...)? Commented Jun 13, 2019 at 17:45
  • have a look at this answer. You can declare the variable in the browser with the ejs value and it'll be available for jquery. Commented Jun 13, 2019 at 17:50
  • 1
    you're using a server to render pages using ejs syntax. 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 for writeInputs.js needs to go to an actual serving route (like app.get('writeInputs.js', ...) if you're using express) and you have to use ejs rendering if you want your ejs syntax to get replaced. Commented Jun 13, 2019 at 18:56

3 Answers 3

2
+50

Node express first renders HTML and then browser do some requests based on HTML to get scripts, images, etc. For this reason <%= filters.title %> is not interpreted by EJS.

As an alternative render some data into HTML and rewrite JavaScript to read filters.title from HTML, like this:

<input type="hidden" id="filter" value="<%= filters.title %>" />

<script src="yourScript.js"></script>

yourScript.js:

    var filter = document.getElementById("filter").value;
    $("#myDiv").append("<input type='search' value='" + filter + "' name='filters[title]' class='form-control'>");
Sign up to request clarification or add additional context in comments.

Comments

1

Only ejs files inside your views folder are rendered from the server so you will have to create the variable in that file instead. Here is an example:

view.ejs

<script>
    var ejsVal = <%= val %>; // You have to use var to make it global
</script>
<script src="script.js"></script>

script.js

console.log(ejsVal);

1 Comment

You have to write your ejs syntax inside QUOTES like, "<%= val %>"
0

can you try like this?

var temp = <%= test  %>;
$("#myDiv").append("<input type='search' value='"+temp+"' name='filters[title]' class='form-control'>");

4 Comments

Thanks but it still gets rendered as a string literal: "<%= filters.title %>"
@fugu changed can you try this
But <%= test %> won't be available in my .js file
Sorry put filter value there

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.