1

Let's say I pass a variable myvar from my router to the view-

app.get('/test', function(req, res) {
    res.render('testPage', {
        myVar: true
    });
}

Now I can use this variable in the view within script tag like this -

<script>
  var myVar = <%- JSON.stringify(myVar) %>;
  console.log(myVar); // prints 'true'
</script>

What I want to do is reset the view variable myVar to false however that isn't happening.

<script>
  var myVarJS = <%- JSON.stringify(myVar) %>;
  console.log(myVarJS); // prints 'true'
  <%- myVar = false %>;
  myVarJS = <%- JSON.stringify(myVar) %>;
  console.log(myVarJS); // still prints 'true'
</script>

The scenario is that one can pass view variables from the router with a value. Now I would like to change the value of that variable from my client side javascript.

The templating engine for views that I am using is EJS.

10
  • whats the scenario? Can't you create new variable based on myVar? can you please tell us the use case? Commented Mar 31, 2016 at 5:35
  • @DeendayalGarg i have edited the last code snippet in my question. Commented Mar 31, 2016 at 5:37
  • @DeendayalGarg the scenario is that you can pass view variables from the router with a value.Now i would like to change the value of that variable from my client side javascript.Any more questions? Commented Mar 31, 2016 at 5:38
  • @rnjai you are not trying to change the value from your client side javascript, you are trying to change the value while parsing your template. The question still remains. Why? Commented Mar 31, 2016 at 5:41
  • @trex005 it is a bit too complex too explain.What i want to do is - " the scenario is that you can pass view variables from the router with a value.Now i would like to change the value of that variable from my client side javascript." Commented Mar 31, 2016 at 5:46

1 Answer 1

1

Change <%- myVar = false %>; to <% myVar = false %>;

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.