0

I am passing a gstring as a parameter to a javascript function as shown:

<div class="btn btn-small" id="helpful" onClick="helpful(${value})">Helpful</div>

Here, value comes from controller which is dynamic. I have javascript function as:

function helpful(answer){
        $.post("${createLink(controller: "reviews", action: "dataFromReviews")}",
                {
                    helpful:true,
                    answer:answer
                }
        );
    }

But I am not getting any response. No error while inspecting. Can't we pass gstring in javascript? Please help.

2 Answers 2

1

As @helgew suggests, check the output. Also note that grails >2.3 will HTML encode anything passed in with a GString unless you have disabled this (grails.views.default.code='none'). You'll need to use the raw codec to avoid the encoding. Use with caution:

<div class="btn btn-small" id="helpful" onClick="helpful('${raw(value)}')">Helpful</div>
Sign up to request clarification or add additional context in comments.

Comments

0

What does the generated HTML look like? I would think that you have a javascript error in it because you are not quoting the extrapolated string you are using as a parameter for the 'helpful' function. This should work:

<div class="btn btn-small" id="helpful" onClick="helpful('${value}')">Helpful</div>

1 Comment

Thankyou very much @helgew. This was a silly mistake. I feel embarrassed to have asked it

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.