Sherif
Sherif

Reputation: 431

have access the GSP elements in Jquery

In Grails GSP , is there a way to access the GSP elements in Jquery , for example , i have the below code in the GSP :

<g:hasErrors bean="${adsInstance}">

in Jquery , i want to access adsInstance , is that applicable ?

Upvotes: 0

Views: 159

Answers (2)

Uday
Uday

Reputation: 629

@Sherif The code you written must be in gsp file script tag but this code will not work when you move your code to some javascript file.

The question you asked is about hasErrors, if you want to use hasErrors you can do it in gsp script tag as well (though its not a good practice)

<script>
var result = "${hasErrors(bean:adInstance,'1')}"
</script>

result will have 1 if the instance has any error.

Upvotes: 0

Joshua Moore
Joshua Moore

Reputation: 24776

First, you need to understand that GSP is processed server-side and as such variables in that scope are only available on the server.

Secondly, you need to understand that jQuery is a client-side library which runs on the client.

In order to get the "data" from a server-side variable into a client-side variable you need to dynamically create the jQuery/javascript which is going to be executed.

Something like this perhaps:

// in the GSP page in question, within some block of jQuery/javascript
var someVariableInJavascript = ${adsInstance as JSON}

The above should populate the variable with a javascript object that has the same properties and values (not methods) as your adsInstance

If nothing else, this should put you on the right path.

Upvotes: 3

Related Questions