0

If I have a html document with this:

 <p onclick="hiho('@name')"> clicky </p>


<script>
    function hiho(namevar){
        var1 = @Names.find.where().eq("name", namevar).findUnique();
        if( var1 != null){
            alert("HIHO");
        }
    }
</script>

How do I use the JavaScript variable?

Play won't compile properly because inside

var1 = @Names.find.where().eq("name", namevar).findUnique();

it cannot find the value of namevar.

1
  • 2
    You can't do that. The Scala code is executed on the server, the Javascript core runs on the client. Commented May 31, 2013 at 10:47

2 Answers 2

2

One, pretty straightforward, solution could be:

<p onclick="hiho('@name')"> clicky </p>

<script>
    var map = {
        '@name': '@Names.find.where().eq("name", name).findUnique()'
    };

    function hiho(namevar){
        var value = map[namevar];
        if( value != null){
            alert("HIHO: " + value);
        }
    }
</script>

Supposing that the name variables are strings.

Sign up to request clarification or add additional context in comments.

Comments

2

You'll probably want to make an AJAX request back to the server. This page talks about that some toward the bottom, and about the routing code you'll need.

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.