Pen Settings



CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ 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.


Save Automatically?

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</title>
    <!-- Jquery -->
    <script src="" type="text/javascript"></script>
    <!-- Select 2 -->
    <link href="" rel="stylesheet" />
    <script src=""></script>
  <p>Animal préféré :</p>
  <select id="idAnimal" class="select2"><option value="0">Animal ?</option></select>

  <div id="divResult"></div>




// 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 () {

  // remplissage du select désiré
    data: $.map(locs, function (a) {
      return {
        "text": a.text
  // style global de tous les select 
    width: '100%',
    allowClear: false,
// 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