0

I am trying to set the id on a grails link, dynamically, using javascript. Is that even possible?

<g:link controller:"patient" action:"show" id:"patient0-id"/>

<script type="text/javascript">
   var id = 20;
   $("#patient0-id").attr("id", id);
</script>

1 Answer 1

2

No it's not possible since the Grails tag is processed server-side (before the page is rendered by the client) and JQuery/Javascript is processed client-side in the browser after the Grails tag has already been processed by the server. Thus, you can't use JQuery/Javascript to effect the Grails tag.

However, you could do something like this:

<a id="someLink" href="#">Something</a>

<script type="text/javascript">
  var baseUrl = "${createLink(controller: 'someThing', action: 'someAction'}";
  var id = 1234;
  $("#someLink").attr("href", baseUrl+"/"+id);
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

This is the correct approach; however, be sure you formulate the final link URL according to your URLMappings, if you have changed them from the Grails defaults.

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.