user1584421
user1584421

Reputation: 3843

Javascript accessing ejs variable in Node.js context

I am trying my javascript code inside an HTML file to access an ejs variable.

This is the Node.js code:

res.render('target.ejs', {data:user});

When i do this in HTML:

<p> <h1><%= data %></h1> </p>

evertything is ok.

But when I try to access the variable inside a <script>, like this:

<script>
    var name = <%= data %>;
    alert(name);
</script>

then nothing happens. I also tried with a console.log(name); and still it didn't work.

Upvotes: 0

Views: 452

Answers (1)

Robin C Samuel
Robin C Samuel

Reputation: 1225

Seems like you need some quotes.

Try

  var name = '<%= data %>';

Upvotes: 2

Related Questions