Grails uses 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 the javascript code part.

My solution now (inside GSP):

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

and then I have a javascript block inside my GSP with jQuery Stuff etc., I need this value.

Is there any other way to have grails variables available inside pure javascript code?

And the second question, quite the opposite. I select, for example, from the drop-down list and click "Save", and then I want to save the value of $ ("# select-box"). Val () inside the session variable from the JS part.

Thanks in advance for your help.

Greetings

Marco

+6
source share
2 answers

Why not use javascript GSP tag? The solution might look like this:

 <g:javascript> var jsSelectedValue = "${session.selectedValue}"; </g:javascript> 
+12
source

The solution to the first problem may be as follows:

UPDATE: Modifications as per @Medrod's solution:

 <script type="text/javascript"> var jsSelectedValue = "${session.selectedValue}"; </script> 

And for the second question:
Send the selected value to the server and set the session variable.

+1
source

Source: https://habr.com/ru/post/894627/


All Articles