0

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.

2
  • 1
    View Source ... is your friend. Your browser should show the html generated by node / ejs. Commented Aug 12, 2019 at 18:35
  • I want the variables to be accessible in Javascript. I don't know if they can be seen inside <script>. Neither alert, nor console.log() is working. Commented Aug 12, 2019 at 18:54

1 Answer 1

2

Seems like you need some quotes.

Try

  var name = '<%= data %>';
Sign up to request clarification or add additional context in comments.

Comments

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.