Pen Settings



CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource


Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource


Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.


Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.


    <meta charset="utf-8" />
    <title>JQuery select2 - remplissage auto et cherche dans array</title>
    <!-- Jquery -->
    <script src="" type="text/javascript"></script>
  <label for="idAnimal">Animal préféré :</label>
  <select id="idAnimal" name="idAnimal"><option value=""></option></select>

  <div id="divResult"></div>
  <!-- Select 2 -->
    <link href="" rel="stylesheet" />
    <script src=""></script>


                select {min-width:60%;padding: 4px 0px 0px 8px;border: 1px solid #ccc;border-radius: 4px; font-family: "Poppins";margin:0;font-size:0.9rem;color:#444;}
label {width: 20%;display: inline-block;vertical-align:top;padding:4px 0;}


// init Json
var locs = [];  
// remplissage JSon, via Ajax, dans ce cas je le fais en manuel
item = {};
item["id"] = 1;
item["text"] = "Chien";
item = {};
item["id"] = 2;
item["text"] = "Chat";
// ça nous donne :
// locs = [{"id":1, "text":"chien"},{"id":2, "text":"chat"}];
$(document).ready(function () {

  // style global de tous les select 
width: '70%'
  // remplissage du select désiré
    dropdownAutoWidth: true,
    allowClear: true,    
    placeholder: "Animal ?",
    data: $.map(locs, function (a) {
      return {
        "text": a.text
// on change select => affiche réponse	
$("#idAnimal").on("change", function () {
   var idClicked = this.value; // retrieve Id selected
   $.each(locs, function (i, v) {
      if ( == idClicked) {
         var textClicked = locs[i].text;
        $("#divResult").append("<p> Vous avez choisi : " + textClicked + "</p>"); // display text
         return false; // stops the loop
  //astuce : permet de fermer en auto le select après sélection
  var self = $(this);
  setTimeout(function () {
  }, 1);