phil.e.b
phil.e.b

Reputation: 2621

Passing javascript variable to gsp g:link tag

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>


    <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

Answers (2)

user800014
user800014

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

Sergei Shushkevich
Sergei Shushkevich

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

Related Questions