FrancescoDS
FrancescoDS

Reputation: 995

get values selected in g:select tag

In my grails project I have the following select:

<g:select name="receiptItems" multiple="multiple"/>

I want to get values currently selected when a checkbox is checked or unchecked. I do the following:

 <g:checkBox name="hasStampDuty" value="${receiptInstance?.hasStampDuty}"
            onclick="${remoteFunction(
                    controller: 'Receipt',
                    action: 'addStampDuty',
                    params: '\'receiptItemsSelected=\' + document.getElementById(\'receiptItems\').value+\'&receiptInstance=' + receiptInstance+'&ischecked=\'+this.checked',
                    onSuccess: 'updateTotalAmount(\'totalAmount\', data, \'00000\')')}"
 />

but document.getElementById(\'receiptItems\').value returns only the first selected value of the g:select.

I've also tried with $(\'receiptItems\') or $(\'"receiptItems"\') or jQuery(\'receiptItems\') but they do not work as expected.

EDIT: I've also tried with jQuery(\'receiptItems\').val() and it does not work

How can I solve this?

Upvotes: 0

Views: 2355

Answers (1)

echohtp
echohtp

Reputation: 146

your selector is wrong, try this following (not i dont know how grails works):

$('select[name="receiptItems"]').val();

The first gives the type of element youre looking for and the second part is the unique name of that element. I personally am not a fan of using names and prefer ID's for important things; were you to change your first line to

<g:select id="receiptItems" multiple="multiple"/>

You can select and get its value using:

$('#receiptItems').val()

HTH

Upvotes: 2

Related Questions