RohanK
RohanK

Reputation: 67

Selecting multiple value from a dropdown and call a javaScript function

I know I can create a multiple select dorpdown like this -

 <select name="city" multiple>
      <option value="Dallas">Dallas</option>
      <option value="New York">New York</option>
      <option value="Kansas">Kansas</option>
      <option value="Massachusetts">Massachusetts</option>
 </select>  

Using this multiple select dropdown I can select multiple city from. But can I select a javascript method separately for each of the city?

Thanks in advance.

Upvotes: 0

Views: 2130

Answers (2)

Adi Darachi
Adi Darachi

Reputation: 2175

You can get the value using jQuery '.val()' function. Jquery.fn.val()

You can see how it works in the following example:

http://jsfiddle.net/uoL3s610/

$(document).ready(function(){
    $('[name="city"]').change(function(){
        $('#selected_container').text($('[name="city"]').val());
   });
});

Or this example

http://jsfiddle.net/uoL3s610/1/

$(document).ready(function(){
$('[name="city"]').change(function(){
    $('#selected_container').text('');
    $.each($('[name="city"]').val(),function(index,value){
        $('#selected_container').text($('#selected_container').text() + ', ' + value)
    });
  });
});

Upvotes: 2

Tom Nijs
Tom Nijs

Reputation: 3950

You are able to store the selected value of your dropdownlist in a variable and then continue with an If-structure or a switch:

$("city:selected").each(function () {
    switch($(this).val()){
        case "city1":
        break;
    }
});

Upvotes: 1

Related Questions