Getting multiple selected options with javascript from collection_select, RoR

467 Views Asked by At

I have a view in my Ruby on Rails application where there are two collect_selects on the right side, one of which is a list of users and the other a list of objects which has :multiple => true. When the user has selected a user and at least one object I want to display a chart on the right side with the user and whichever objects have been selected (likely using the gem Flot). I have seen how to get the selected object using selected_index, but I'm stumped as to how you can get an array of all of the selected objects when multiple selections are allowed. I was hoping to do everything in the javascript without having a submit button or anything, but is this possible?

1

There are 1 best solutions below

0
On BEST ANSWER

I finally found my own answer. To get all of the selected objects in javascript, for example in a collection_select with id "user_id" you just need to use:

$("#user_id option:selected").each(function() {

    // Do whatever with $(this).text() or $(this).val() for each option.

});