maaz
maaz

Reputation: 3664

Not able to get the selected values from Drop down using jQuery

I'm trying to add new field to existing gsp page which was written by some other guy.

    <g:select name="clientId" multiple = "yes" size = "4" from="${com.mycompany.fleet.partymodel.roles.ClientRole.list()}" class = "filter_combo" optionKey="id" />

and by using jQuery he is getting selected values like this

var selectedclients = "${clientId}";
console.log(selectedclients); // prints id of selected Clients 

I'm adding new drop down like this in same page

<g:select name="contractId"  multiple = "yes" size = "4" from="${com.mycompany.fleet.rules.Contract.list()}" class = "filter_combo" optionKey="id"/>

and I'm trying to get the selected values using jQuery like this

var selectedcontracts = "${contractId}";

but its not fetching the selected values... am I doing anything wrong here?

What does the ${clientId} mean exactly?

Upvotes: 0

Views: 967

Answers (2)

Shef
Shef

Reputation: 45589

How about:

var selectedcontracts = $('select [name="contractId"]').val();

Upvotes: 0

tim_yates
tim_yates

Reputation: 171184

I don't believe that is using JQuery, that is just putting the current value for clientId into the javascript for var selectedclients = "${clientId}"; using standard GSP templating.

ie: when the page is generated, ${clientId} gets replaced with the value in the model

Are you adding contractId to the model in the Controller?

One thing to watch out for here is that if the clientId or contractId has a double quote in them (ie, if clientId was Tim"busy" Yates, then this will generate invalid javascript, as the generated code would look like:

var selectedclients = "Tim "working" Yates" ;

However, if it's just an integer id then I guess this would be ok

Upvotes: 2

Related Questions