Search code examples
htmlgrailsgroovygsp

How do i use a variable value as the id of an HTML element on a gsp page?


I am trying to dynamically set the id values of some elements on a gsp page. The values are passed to the page from the controller when I render it. However, when I look at the source code, the value of the id is not set.

I am trying to do the following:

<g:each in="${listBenefits}" var="benefit" status="i">
    <div class="card"  id="${benefit.getId()}">
...
</g:each>

but the value is not inserted into the id.

I can, however, do the following:

<h1>${benefit.getId()}</h1>

And the value is displayed.

I have tried to set the value to a variable before using it like this

<g:set var="idaccordion" value="${benefit.getId()}" />
<div class="card"  id="${idaccordion}">

and does not work either.

I am expecting to get something like this:

<div class="card"  id="desired_id">

and instead I get something like this

<div class="card"  id="">

Any ideas of what I am doing wrong would be appreciated.


Solution

  • I am unable to find any mistakes in your work. I suggest you try the following snippet in the same view (i.e. gsp page) and see how it is rendered.

    <g:each in="${1..3}" var="rn">
        <div id="${rn}">Test ${rn}</div>
    </g:each>
    

    If the code works properly, I reckon you need to revise benifit.getId() method. You're able to use benefit.id to see whether it gives any difference.