tags:

views:

330

answers:

3

I want to make an Ajax call using remoteLink (with Prototype as the Javascript library) but I need one of the parameters being passed to be the value from a textfield. Here's what I have so far in my GSP:

<input id="email" name="email" type="text"/>
...
<g:remoteLink action="addEmail" params="[email:???]">Add</g:remoteLink>

What do I put in place of ??? to get the remoteLink to send the value of the email textfield as a parameter? Essentially, how do I reference/access the DOM within a Grails tag?

I tried putting

\$('email').value

in place of ??? but I got a "Could not parse script" error in my GSP.

Thanks

+1  A: 

<g:remoteLink action="addEmail" params="${[email: some.groovy.to.get.your.email()]}">Add</g:remoteLink>

John Stoneham
A: 

If you are okay with using a button instead of a link this becomes trivial using the g:submitToRemote. But, if it has to be a link, you can do something ugly like this:

The views/email/index.gsp:

<%@ page contentType="text/html;charset=UTF-8" %>

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Sample title</title>
    <g:javascript library="prototype" />
  </head>
  <body>
  <g:form name="theForm">
    Email: <g:textField name="emailAddr" />
    <!-- Here comes the ugly -->
    <a href="#" name="submit" 
       onclick="new Ajax.Updater('resp','${createLink(action:'addEmail')}',{asynchronous:true,evalScripts:true,parameters:Form.serialize(document.theForm)});return false">
    Submit Form
    </a>
  </g:form>
  <div id="resp">
  </div>
  </body>
</html>

The email controller:

class EmailController {

    def index = { }

    def addEmail = {

        if(params?.emailAddr) {
            render "${params.emailAddr}"
        }
        else {
            render "No Email Entered"
        }
    }
}

Couple things to note if you customize this: * the first 'resp' argument to Ajax.Updater is the id of the div that you want to update * in the Form.serialize(document.theForm) command, the 'theForm' needs to correspond with the name you assign the form.

proflux
+3  A: 

I've need to do done something similar before and the following worked for me (yep, not especially elegant):

<input id="email" name="email" type="text"/>
...
<g:javascript>
    var addEmail = function()
    {
        ${ remoteFunction (action:"addEmail", update:"update-element-id", params:"  'email=' +$('email').value  ") }
    };
</g:javascript>
<a href="javascript:void(0)" onclick="addEmail();return false;">add email</a>

Extracted to a javascript function for clarity, and added some spaces inside the params to show the single quotes clearer.

Chris
This is better than the way I outlined; it allows you to switch from Prototype to something else without breaking the implementation. Good suggestion, upvoted.
proflux
If you move the var emailVal = $('email').value;line inside the function this works really well. Can you please make the change in your answer and I'll accept this. Thanks.
Everett Toews
done. thx for up proflux, wish there was a more elegant way to get gsp and javascript to interact, guess its difficult with one being server and other client...
Chris