Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails using grails var in GSP Site inside javascript

I have a question using grails variable values in javascript code in a GSP file.

For Example: I have a session value session.getAttribute("selectedValue") and I want to use this value inside javascript code part.

My solution is now (inside a GSP):

<%
    def js = new String("<script type=\"text/javascript\">")
    js += "var jsSelectedValue = " + session.getAttribute("selectedValue") + ";"
    js += "</script>"
    out << js
%>

and then I have javascript block inside my GSP with jQuery Stuff and so on, there I need this value.

Is there another way to have grails variables accessible inside pure javascript code?

And second question, the exactly other way around. I select for example in a dropdown box and click "save" and then i want to store the value $("#select-box").val() inside a session variable from JS-part.

Thank you very much in advance for your help.

Cheers,

Marco

like image 200
grailsInvas0r Avatar asked Aug 09 '11 12:08

grailsInvas0r


1 Answers

Why do not use the javascript GSP-tag? A solution can look like this:

<g:javascript>
    var jsSelectedValue = "${session.selectedValue}"; 
</g:javascript>
like image 63
Medrod Avatar answered Sep 30 '22 16:09

Medrod