1

I hope that someone can assist.

I have following code which is not accepted:

<g:link title="Periodendaten speichern" action="speicherePeriodendaten" params="'\'pStart=\' + $('#periodeStart'${i}').value'" id="${zwzPeriode.id}">

With $('#periodeStartX'), I am trying to get the DOM element with the ID periodeStartX, where X is the current value of i. i is coming from a g:each-Tag (status=i)

I know that following is working:

<g:link title="Periodendaten speichern" action="speicherePeriodendaten" params="'\'pStart=\' + this.value'" id="${zwzPeriode.id}">

But in the above scenario, I don't want to have the value of the current Tag, I want the value of another Tag.

My problem is therefore, that I don't understand how to escape here correctly.

I would appreciate any good hint.

Best Regards, Michael

2
  • are you sure that params="'\'pStart=\' + this.value'" is working? what do you get on client side for such code? Commented Feb 27, 2013 at 11:22
  • Not any more... At least in a g:link. I have seen that I have used it this way in g:remoteFunction-Tag and g:link is ignoring params attribute? At least in Firebug I cannot see that params is used. And furthermore, g:link is evaluated once when response is generated, so I think have to combine here again g:link with g:remoteFunction Commented Feb 28, 2013 at 9:17

2 Answers 2

2

Another approach below:

<r:require module="jquery"/>

<input id="periodeStart1" value="V1" />
<input id="periodeStart2" value="V2" />

<%-- Sample variables --%>
<g:set var="i" value="1" />
<g:set var="id" value="42" />               

<%-- Resolve pStart "onclick" event --%>                
<a href="${createLink(action: 'speicherePeriodendaten', params: [id: zwzPeriode.id])}" onclick="this.href=this.href+'?pStart='+$('#periodeStart${i}').val()">Sample Link</a>

Edit1: Also, you should probably wrap $('#periodeStart${i}').val() with encodeURIComponent(). Just in case.

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

1 Comment

This looks good, changing the href when click event happens. Thx
1

You will need to escape the $ sign as Groovy will try to treat it as an expressions:

<g:link title="Periodendaten speichern" action="speicherePeriodendaten" params="'\'pStart=\' + \$('#periodeStart'${i}').value'" id="${zwzPeriode.id}">

1 Comment

This I have tried, but what I have learned now is that params is evaluated at the response generation and not at the time where user clicks on the link. So the javascript can never run at the time where user clicks on it. So, I think g:link is not usable here at all.

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.