Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the value of a multiple option dropdown?

Say I have this dropdown:

<select name="color" multiple="multiple">
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
</select>

So basically more than 1 color can be selected. What I'd like is that if a user selects red, and then clicks green, i'd like a function to be called each time which pops up a message box saying the color which was most recently clicked.

I've tried this:

<option value="red" onclick="alert('red');">Red</option>
<option value="green" onclick="alert('green');">Green</option>
<option value="blue" onclick="alert('blue');">Blue</option>

This works in firefox and chrome, but not in IE.

Any ideas?

like image 314
Ali Avatar asked Feb 14 '10 15:02

Ali


3 Answers

$("select[name='color']").change(function() {
    // multipleValues will be an array
    var multipleValues = $(this).val() || [];

    // Alert the list of values
    alert(multipleValues[multipleValues.length - 1]);
});

Here's another examples: http://api.jquery.com/val/

like image 145
Thiago Belem Avatar answered Sep 20 '22 06:09

Thiago Belem


The following code should do what I think you're after. Each time an item is selected, it compares the current list of selections against the previous list, and works out which items have changed:

<html>
  <head>
    <script type="text/javascript">
      function getselected(selectobject) {
        var results = {};
        for (var i=0; i<selectobject.options.length; i++) {
          var option = selectobject.options[i];
          var value = option.value;
          results[value] = option.selected;
        }
        return results;
      }

      var currentselect = {};

      function change () {
        var selectobject = document.getElementById("colorchooser");
        var newselect = getselected(selectobject);
        for (var k in newselect) {
          if (currentselect[k] != newselect[k]) {
            if (newselect[k]) {
              alert("Option " + k + " selected");
            } else {
              alert("Option " + k + " deselected");
            }
          }
        } 
        currentselect = newselect;
      } 
    </script>
  </head>
  <body>
    <select id="colorchooser"
            name="color"
            multiple="multiple"
            onchange='javascript:change();'
            >
      <option value="red">Red</option>
      <option value="green">Green</option>
      <option value="blue">Blue</option>
    </select>
  </body>
</html>

It should work just as well in Internet Explorer as Firefox et al.

like image 37
Tim Avatar answered Sep 23 '22 06:09

Tim


Since you using jQuery,I suggest you to take a look at this superb plugins. This plugins will transform a multiple select dropdown into a checkbox list, so user can select multiple values with easy.

To get the values, I suggest you use fieldValue methods from jQuery form plugins. It's a robust way to get value from any type of form element. Beside, you can use this plugins to submit your form via AJAX easily.

like image 21
Donny Kurnia Avatar answered Sep 22 '22 06:09

Donny Kurnia