0

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.

3
  • 1
    On the change event of select use .val() it will return you an array of selected value. then you call your operation Commented Mar 24, 2015 at 9:16
  • yes, on select event call a javascript method...get the values by the id and iterate over the selected values.. and call different methods there.... Commented Mar 24, 2015 at 9:16
  • 1
    stackoverflow.com/questions/3487263/… this might help you Commented Mar 24, 2015 at 9:17

2 Answers 2

2

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)
    });
  });
});
Sign up to request clarification or add additional context in comments.

Comments

1

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;
    }
});

Comments

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.