user_dev
user_dev

Reputation: 1431

How to select multiple values?

I have a multiple select field where user can select multiple fields. Right now it is taking only the first value of the dropdown.

Here is the jquery code which uses velocity template as front end:

jQuery('select[name="myServices"]').live('change', function(ev) {
            var selectedmyServicesOpts = jQuery(this).find(':selected'),
                    ismyServicesOther=false;
            console.log("Getting all selectedvalue" + selectedmyServicesOpts)
            if(selectedmyServicesOpts.length > 0) {
                jQuery.each(selectedmyServicesOpts, function(index, value) {
                    if(jQuery(value).val() === 'Other (Text field)') {
                        jQuery('.myServicesOther').show();
                        ismyServicesOther=true;
                        return false;
                    }
                });
            }
            if (!ismyServicesOther) {
                jQuery('.myServicesOther').hide();
            }
        });

Upvotes: 0

Views: 109

Answers (1)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324610

For a given <select multiple> element, jQuery("theelement").val() gives an array of selected option values.

In your case, you'd probably want to check:

var values = jQuery(this).val();
if( values.indexOf('Other (Text field)') > -1) {
    jQuery('.myServicesOther').show();
}
else {
    jQuery('.myServicesOther').hide();
}

Upvotes: 1

Related Questions