0

Basically, in the controller, something dynamic happens, and I pass a non-static string to the view:

String token = "";
render(token);

I can easily do:

<div id="token>${token}</div>

...and grab the contents:

$('#token').html();

But what doesn't seem to work is, in the javascript, doing:

function token(token) {
  // do something with token
  console.log('token: ' + token);
}

token(${token});

I can kind of see why it doesn't work...but what is the syntax for this?

1 Answer 1

2

You didn't name your function. It should be:

function token(token) {
  // do something with token
  console.log('token: ' + token);
}
token(${token});

Edit: You may need to just add quotes:

token("${token}");

By the way, this works, but in general I'd avoid using the same name for the function and its argument. A better name might be logToken:

function logToken(token) {
  // do something with token
  console.log('token: ' + token);
}

logToken("${token}");
Sign up to request clarification or add additional context in comments.

4 Comments

@Frank - In that case, you probably are just missing quotes. I've updated my answer.
token: ${token} is what that gives me..which I definitely tried before :)
@Frank - When you use token(${token}) and view source, I suspect you will find that ${token} is being written literally and is not being parsed. I'm not familiar with Play Framework, so I can't provide much guidance on that front. But just to guess, is this in an external .js file? Are other Play tags in the same source file being parsed correctly?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.