1

i have a javascript function as follows

function GetSelectedItem()
{
    var e = document.getElementById("country");
    var strSel =  e.options[e.selectedIndex].value;
    alert(strSel);
    var url = "${createLink(controller:'country', action: 'wholeTestUnits', id: strSel)}"
    alert(url);
 }

i want to go to that url action when i click the submit button like

<button class="submit_small" onClick="GetSelectedItem();">
    <span><g:message code="default.button.submit.label" /></span>
</button>

This ${createLink} is not working.

2
  • 1
    is your url is showing in your alert or not? Commented Sep 12, 2013 at 11:05
  • Is this function on GSP page? Commented Sep 12, 2013 at 16:34

6 Answers 6

3

A better way of doing this which doesn't require the JavaScript code be in your GSP would be the following:

<button class="submit_small" onClick="GetSelectedItem();" data-url="${createLink(controller:'country', action: 'wholeTestUnits')}">
    <span><g:message code="default.button.submit.label" /></span>
</button>

function GetSelectedItem() {
    var button = event.target;
    var e = document.getElementById("country");
    var strSel =  e.options[e.selectedIndex].value;
    var url = button.getAttribute("data-url") + "/" + strSel;
}
Sign up to request clarification or add additional context in comments.

Comments

1

I think you have a serverside/clientside problem. The createLink is run on the server, the JS is run on the client...

Try:

var url = '${createLink(controller:'country', action: 'wholeTestUnits')}' + strSel ;

3 Comments

Is that function in a JS script or a GSP? Can you quantify 'not working'? Just saying "It's not working" doesn't really help anyone to help you...
Its a javascript function inside a gsp page
So what error do you get? What does the generated javascript look like if you view source of the page?
1

As I think , you are not getting value of strSel in your link. You can try this.

function GetSelectedItem()
{
        var e = document.getElementById("country");
        var strSel =  e.options[e.selectedIndex].value;
        alert(strSel);
        var url = "${grailsApplication.config.grails.serverURL}/country/wholeTestUnits/" + strSel
        alert(url);
}

1 Comment

You need to define that property before use it
0

Instead of using the createLink you could build your own URL and use that one instead. You have to pay attention to capital letters in the controller name, though.

var url="${ createLink(controller:'testcontroller', action:'getData') }";

is equivalent to

var url = "/testcontroller/getData;

If you want to pass in arguments from javascript to the controller you can do like this.

var url = "/testcontroller/getData?arg0=" + arg0 + "&arg1=" + arg1;

To extract the arguments in the controller you do use the params keyword. So to print the parameters in the controller you do this:

println params.arg0
println params.arg1

1 Comment

If in cookieless mode you won't get a session id added to url then.
0
    --in gsp file--
   <a href="#" onclick="callAjax('${createLink(controller:'shift',action: 'addShift')}');" >Add/Edit Shift</a>


    --in js file--
    function callAjax(path){
    //path->/shift/addShift
       var xmlhttp = new XMLHttpRequest();
                xmlhttp.onreadystatechange = function() {
                    if (this.readyState == 4 && this.status == 200) {
                        document.getElementById("updateContent").innerHTML = this.responseText;
                    }
                };
                xmlhttp.open("GET", path, true);
                xmlhttp.send();
      }

Comments

0

try this: var url = "${createLink(controller:'country', action: 'wholeTestUnits', params:[id: strSel], absolute: true)}"

Comments

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.