Title: Multiple values select2
Last modified: May 2, 2017

---

# Multiple values select2

 *  [saurav.rox](https://wordpress.org/support/users/sauravrox/)
 * (@sauravrox)
 * [9 years, 1 month ago](https://wordpress.org/support/topic/multiple-values-select2/)
 * Hi,
    I have been trying to show multiple selected values in select2 dropdown.
   So, far I have developed the dropdown and saved the selected values. I have the
   following code as the options for the dropdown: `<option value="<?php echo esc_attr(
   $post->ID);?>"><?php the_title();?></option>` **Note**: The options are on loop.
 * Now, I can not set those multiple values to select2 on page load. Those multiple
   values only set if option is selected from the dropdown again.
 * My code is mentioned below:
 *     ```
       if ( isset( $wp_travel_engine_setting['crosssell']['trips'] ) && $wp_travel_engine_setting['crosssell']['trips']!='' )
       {
       	$selectedval = $wp_travel_engine_setting['crosssell']['trips'];
       }
       echo
       '<script>
       jQuery(document).ready(function($){
       	$("#wte-cross-sell-'.$post->ID.'").select2();
       	var arrayFromPHP = '.json_encode($selectedval).';
       	console.log(arrayFromPHP);
       	$("#wte-cross-sell-'.$post->ID.'").val(arrayFromPHP);
       	$("#checkbox").click(function(){
       		if($("#checkbox").is(":checked") ){
       	    	$("#wte-cross-sell-'.$post->ID.' > option").prop("selected","selected");
       	    	$("#wte-cross-sell-'.$post->ID.'").trigger("change");
       		}else{
       	    	$("#wte-cross-sell-'.$post->ID.' > option").removeAttr("selected");
       	     	$("#wte-cross-sell-'.$post->ID.'").trigger("change");
       	 	}
       	});
       });</script>';
       ```
   
 * //variable $selectedval will get us the array of selected value ids like this
   as seen on browser console:
    `(2) ["1301", "1024"]`
 * Can anyone shade some light on this? How can I set the values on pageload without
   choosing the option again.
 * Thanks for your time!
 * Kind regards,

Viewing 1 replies (of 1 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [9 years, 1 month ago](https://wordpress.org/support/topic/multiple-values-select2/#post-9090719)
 * I’m not quite sure off the top of my head how to do so with jQuery, but in PHP,
   do like so:
 *     ```
       <option value="<?php echo esc_attr( $post->ID );?>"
          <?php selected( in_array( $post->ID, $selectedval )); ?>><?php the_title();?>
       </option>
       ```
   
 * This addition outputs “selected” if the post ID in the $selectedval array of 
   IDs

Viewing 1 replies (of 1 total)

The topic ‘Multiple values select2’ is closed to new replies.

## Tags

 * [select2](https://wordpress.org/support/topic-tag/select2/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 1 reply
 * 2 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [9 years, 1 month ago](https://wordpress.org/support/topic/multiple-values-select2/#post-9090719)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
