0

I understand that SELECT multiple will let a viewer select multiple items in a dropdown list and then the form submission passes all the selected items. But how does this work in JavaScript when there is no form submission and you're just watching for a select change:

$('#delete_dropdown').change(function(e){

and then looking at $(e.target).val() for the selected values?

Thanks

1 Answer 1

1

You can look at this context inside your handler and val() will return the array of elements, with jquery you can do this:

   $('#delete_dropdown').change(function(e){
        var selectedArray = $(this).val();  //Array of selected values
   });

Fiddle

If you are going with vanilla javascript you will need to loop through the options to check which all are selected and add them to suit yourself.

Sign up to request clarification or add additional context in comments.

2 Comments

That's beautiful! Thanks. I'm fine with jQuery so I can do the array thing. One thing I notice about your jsfiddle (I added a few more options so it's up to jsfiddle.net/Z6KWr/2 now) is that you get called every click (natually, since it's a "change" handler) but is there some clue when they're finally finished with their selection?
@Steve yes you are right, change event gets triggered when its value is changed, so every selection /unselection triggers change. You can't assure that when they're finally finished with their selection, because selection/deselection can trigger change, so you would have to manually check that in the change handler if the array length is equal to the length of options then all are selected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.