Jay Patel
Jay Patel

Reputation: 325

Select multiple options in select dropdown

how can I select multiple value in this country dropdown

this is my code:

function print_country(country_id) {
  // given the id of the <select> tag as function argument, it inserts <option> tags
  var option_str = document.getElementById(country_id);
  option_str.length = 0;
  option_str.options[0] = new Option('Select Country', '');
  option_str.selectedIndex = 0;
  for (var i = 0; i < country_arr.length; i++) {
    option_str.options[option_str.length] = new Option(country_arr[i], country_arr[i]);
  }
}

Upvotes: 0

Views: 5753

Answers (3)

m.bouali
m.bouali

Reputation: 484

Use the option.selected property like this:

var option_str = document.getElementById('country_id');
for ( var i = 0; i < option_str.options.length; i++ )
{
  if ( //condition for selecting the current option)
  {
    option_str.options[i].selected = true;
  }
}

Upvotes: 1

Waqas Ali
Waqas Ali

Reputation: 114

<select class="form-control select2 color-gray" multiple="multiple">
    <option>Option 1</option>
    <option>Option 2</option>
    <option>Option 3</option>
    <option>Option 4</option>
    <option>Option 5</option>
</select>

Jquery

$(".select2").select2();

you can download select2 plugin on github. link mention on below. https://github.com/select2/select2/tree/develop/dist/js

Upvotes: 0

ztadic91
ztadic91

Reputation: 2804

You can add the multiple attribute to your existing select. It allows you to select multiple values with the CTRL button.

<select multiple></select>

working example here

Upvotes: 1

Related Questions