Search code examples
javascriptgrailsgsp

Pass an instance property of a Groovy Server Pages to a javascript function


Here is the snippet of code:

<li>
    <g:link controller="Customer" action="customerInfo" id="${CustomerInstance.id}"  onclick="return confirm('${message(code: 'default.button.customerInfo.confirm.message', default: 'Customer Info' ,args:['${CustomerInstance.id}'])}');">Customer Information</g:link>
</li>

What the code does is whenever a user clicks the link, an alert box should come with the customer id.

This code works for me:

<li>
    <g:link controller="Customer" action="customerInfo" id="${CustomerInstance.id}"   onclick="return confirm(Customer ID '+${CustomerInstance.id}+'.Are you sure to continue? ');">Customer Information</g:link>
</li>

I think there is something wrong with the quotes.Can somebody help me out??


Solution

  • Don't use the single quotes or GSP expression in your 'args' parameter. You can just do the following: args:[CustomerInstance.id]

    Full example:

    <li>
        <g:link controller="Customer" action="customerInfo" id="${CustomerInstance.id}"  onclick="return confirm('${message(code: 'default.button.customerInfo.confirm.message', default: 'Customer Info' ,args:[CustomerInstance.id])}');">Customer Information</g:link>
    </li>