Reputation: 3843
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
Reputation: 1225
Seems like you need some quotes.
Try
var name = '<%= data %>';
Upvotes: 2