0

How can I pass a javascript value to grails variables. For example:

   var ert = 1;
   function test(){
   ert++;
   }

that function will be called if a button is click, does incrementing the variable. Now I want to pass this value to a grails variables or groovy variables that is being returned to the page. How can I do this?

2
  • How do you want to use this variable? what do you mean with 'returned to the page'? Commented Oct 26, 2012 at 1:31
  • I have an action such as def mypage(){ def inc = 0; [inc:inc]}. I want to copy the javascript value to the inc variable to which the mypage contains the function test() Commented Oct 26, 2012 at 1:34

1 Answer 1

1

Add a hidden field to your form and when the button is clicked, modify that hidden element's value to be what you want sent to the server.

<button onclick="testFunction()">Click me</button>
<input type="hidden" name="inc" id="inc" value="1" />
<div id="testOutput">1</div>​

var ert = 1;
function testFunction() {
    var inc = document.getElementById("inc");
    ert++;
    inc.value = ert;

    //for demo only
    var testOutput = document.getElementById("testOutput");
    testOutput.innerHTML = inc.value;
}​

jsFiddle example

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

1 Comment

what do you mean? can you please give atleast sample code for it? thanks.

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.