Reputation: 2621
I am looking to pass a javascript variable in the id field of a g:link gsp tag but don't know how to do it. If I am going about this the wrong way let me know.
Here is what I have:
<r:script disposition="head">
var data = -1;
$(document).ready(function () {
$('#${tableId} tr').click(function (event) {
data = $(this).attr('id');
});
});
</r:script>
data
variable to a g:link
<li><g:link controller="filterIntraday" action="show" id="??????">View</g:link></li>
I can't get this to work. How do I set the javascript variable in the gsp tag. Or what is another way to go about passing a client side variable in the g:link tag ?
Thanks.
Upvotes: 0
Views: 3996
Reputation:
You cannot set the id in the gsp tag because the tag is already processed. What you can do is change the href
of the generated html. To do that you need to assign an id to the <a>
element.
<li><g:link controller="filterIntraday" action="show" elementId="linkToFilter">View</g:link></li>
This will create something like:
<li>a<a href='yourapp/filterIntraday/show' id="linkToFilter" /></li>
Then in your javascript you can change the href:
data = $(this).attr('id');
$link = $('#linkToFilter');
$link.attr('href', $link.attr('href')+'/'+data);
Upvotes: 3
Reputation: 1376
How about changing "href" attribute inside event function:
function (event) {
data = $(this).attr('id');
$("link-selector").attr("href", "${createLink(controller: 'filterIntraday', action: 'show')}/" + data);
}
Upvotes: 3