Javascript: handler for multi-select dropdown

I have a multi-select dropdown, eg:
<select id="mySelect" multiple="multiple" onchange="myFunction(event)">
  <option value="Audi">Audi
  <option value="BMW">BMW
  <option value="Mercedes">Mercedes
  <option value="Volvo">Volvo
</select>
What will the myFunction look like? I've found examples with single-select but not multi-select.
The user would have to use Ctrl + Click or Shift + Click to select multiple options. Let's say for now, i just want to print out the values selected to the console.
renisenbbAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

renisenbbAuthor Commented:
Also, it doesn't have to be an onchange event. I imagine that for a multi-select, the user would use Ctrl+Click to select a couple of options and then has to click outside the dropdown to exit the dropdown. I don't see any Javascript event for capturing the click outside of the event.
0
Vimal DMSenior Software EngineerCommented:
Hi,

var foo = []; 
$('#multiple :selected').each(function(i, selected){ 
  foo[i] = $(selected).text(); 
});

Open in new window

0
Julian HansenCommented:
Here are two method for doing this
JQuery
<script>
$(function() {
  $('#mySelect').change(function() {
    $('#JQResult').html('');
    $('option:selected', $(this)).each(function() {
      $('#JQResult').append(
        $('<li/>').html($(this).val())
      );
    });
  });
});
</script>

Open in new window

Plain JavaScript
<script>
function myFunction(element, event)
{
  var result = document.getElementById('JSResult');
  while(result.firstChild) result.removeChild(result.firstChild);
  var values = [];
  for (var i = 0; i < element.options.length; i++) {
    if (element.options[i].selected) {
      var li = document.createElement('li');
      li.appendChild(document.createTextNode(element.options[i].value));
      result.appendChild(li);
    }
  }
}
</script>

Open in new window


Working sample here
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

renisenbbAuthor Commented:
Vimal, i tried it but it didn't work. What's wrong with the code below. I'm just trying to print out the first option that was chosen
Please see attached as i couldn't paste it into this textarea without warnings.
try.html
0
Julian HansenCommented:
You can use the onblur event to capture a navigation away from the control
JQuery source
  $('#mySelect').blur(function() {
    $('#JQResultBlur').html('');
    $('option:selected', $(this)).each(function() {
      $('#JQResultBlur').append(
        $('<li/>').html($(this).val())
      );
    });
  });

Open in new window


Updated sample here
0
Rainer JeschorCommented:
Hi,
Virmals code uses jQuery which I assume that you have not loaded.
Second your HTML file attached to your previous comment is not valid in regards to a proper DOM (missing HTML tag, missing HEAD, missing closing Option tags ...).

Pretty much the same sample as Julian has posted:
function myFunction() {
    var selectBox = document.getElementById("mySelect");
    for (var i=0, len=selectBox.options.length; i<len; i++) {
        if (selectBox.options[i].selected) {
            console.log("Option " + selectBox.options[i].value + " is selected.");
        }
    }
}

Open in new window

Live sample:
http://jsfiddle.net/EE_RainerJ/nd0evev0/

HTH
Rainer
0
renisenbbAuthor Commented:
Thanks very much all of you.
0
Julian HansenCommented:
You are welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.