user1577161
user1577161

Reputation: 175

Passing javascript var value to grails variable

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?

Upvotes: 0

Views: 1190

Answers (1)

Gregg
Gregg

Reputation: 35864

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

Upvotes: 1

Related Questions