{"id":185,"date":"2017-10-16T09:27:31","date_gmt":"2017-10-16T07:27:31","guid":{"rendered":"https:\/\/numa-bord.com\/miniblog\/?p=185"},"modified":"2017-10-16T09:27:31","modified_gmt":"2017-10-16T07:27:31","slug":"jquery-rendre-loption-dun-select-permanente-plugin-select2","status":"publish","type":"post","link":"https:\/\/numa-bord.com\/miniblog\/jquery-rendre-loption-dun-select-permanente-plugin-select2\/","title":{"rendered":"jQuery : rendre l&rsquo;option d&rsquo;un select permanente avec le plugin \u00ab\u00a0select2\u00a0\u00bb"},"content":{"rendered":"<p>Le plugin jQuery <a href=\"https:\/\/select2.github.io\/\">\u00ab\u00a0Select2\u00a0\u00bb<\/a> permet d\u2019am\u00e9liorer les champs de type \u00ab\u00a0select\u00a0\u00bb (liste d\u00e9roulante). Une de ces principales fonctionnalit\u00e9 et d&rsquo;int\u00e9grer un champs de recherche \u00e0 la liste. Tr\u00e8s pratique pour retrouver et s\u00e9lectionner un \u00e9l\u00e9ments dans une longue liste.<\/p>\n<p>Nous allons voir ci-dessous comment mettre en place une astuce afin qu&rsquo;une option de la liste reste toujours disponible. Afin qu&rsquo;un r\u00e9sultat de secours soit toujours s\u00e9lectionnable quand on recherche un \u00e9l\u00e9ment inexistant.<\/p>\n<p>Notre exemple consiste \u00e0 s\u00e9lectionner un instrument dans la liste, avec la possibilit\u00e9 toujours visible de s\u00e9lectionner \u00ab\u00a0Autre instrument\u00a0\u00bb si celui que l&rsquo;on recherche n&rsquo;est pas disponible. On en profitera pour afficher des champs suppl\u00e9mentaires quand cette option est s\u00e9lectionn\u00e9 afin de pouvoir r\u00e9colter les informations concernant l&rsquo;instrument r\u00e9ellement recherch\u00e9.<\/p>\n<p><a href=\"https:\/\/numa-bord.com\/labo\/select2-option-permanente\/\">L&rsquo;exemple est disponible ici<\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-188\" src=\"https:\/\/numa-bord.com\/miniblog\/wp-content\/uploads\/2017\/10\/select2-option-permanente.jpg\" alt=\"\" width=\"1156\" height=\"226\" srcset=\"https:\/\/numa-bord.com\/miniblog\/wp-content\/uploads\/2017\/10\/select2-option-permanente.jpg 1156w, https:\/\/numa-bord.com\/miniblog\/wp-content\/uploads\/2017\/10\/select2-option-permanente-300x59.jpg 300w, https:\/\/numa-bord.com\/miniblog\/wp-content\/uploads\/2017\/10\/select2-option-permanente-768x150.jpg 768w, https:\/\/numa-bord.com\/miniblog\/wp-content\/uploads\/2017\/10\/select2-option-permanente-1024x200.jpg 1024w, https:\/\/numa-bord.com\/miniblog\/wp-content\/uploads\/2017\/10\/select2-option-permanente-624x122.jpg 624w\" sizes=\"auto, (max-width: 1156px) 100vw, 1156px\" \/><\/p>\n<p>Comme d&rsquo;habitude on commence par inclure jQuery, le plugin select2 et un fichier qui contiendra notre script dans le head de la page :<\/p>\n<div class=\"codecolorer-container html4strict default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"html4strict codecolorer\"><span class=\"sc-1\">&lt;!-- jQuery --&gt;<\/span><br \/>\n<span class=\"sc2\">&lt;<a href=\"http:\/\/december.com\/html\/4\/element\/script.html\"><span class=\"kw2\">script<\/span><\/a> <span class=\"kw3\">src<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;.\/js\/jquery-3.2.1.min.js&quot;<\/span>&gt;&lt;<span class=\"sy0\">\/<\/span><a href=\"http:\/\/december.com\/html\/4\/element\/script.html\"><span class=\"kw2\">script<\/span><\/a>&gt;<\/span><br \/>\n<span class=\"sc-1\">&lt;!-- select2 --&gt;<\/span><br \/>\n<span class=\"sc2\">&lt;<a href=\"http:\/\/december.com\/html\/4\/element\/link.html\"><span class=\"kw2\">link<\/span><\/a> <span class=\"kw3\">href<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/select2\/4.0.4\/css\/select2.min.css&quot;<\/span> <span class=\"kw3\">rel<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;stylesheet&quot;<\/span> <span class=\"sy0\">\/<\/span>&gt;<\/span><br \/>\n<span class=\"sc2\">&lt;<a href=\"http:\/\/december.com\/html\/4\/element\/script.html\"><span class=\"kw2\">script<\/span><\/a> <span class=\"kw3\">src<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/select2\/4.0.4\/js\/select2.min.js&quot;<\/span>&gt;&lt;<span class=\"sy0\">\/<\/span><a href=\"http:\/\/december.com\/html\/4\/element\/script.html\"><span class=\"kw2\">script<\/span><\/a>&gt;<\/span><br \/>\n<span class=\"sc-1\">&lt;!--custom --&gt;<\/span><br \/>\n<span class=\"sc2\">&lt;<a href=\"http:\/\/december.com\/html\/4\/element\/script.html\"><span class=\"kw2\">script<\/span><\/a> <span class=\"kw3\">src<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;.\/js\/main.js&quot;<\/span>&gt;&lt;<span class=\"sy0\">\/<\/span><a href=\"http:\/\/december.com\/html\/4\/element\/script.html\"><span class=\"kw2\">script<\/span><\/a>&gt;<\/span><\/div><\/div>\n<p>On met en place notre html de mani\u00e8re classique, avec une option de valeur \u00ab\u00a0autre\u00a0\u00bb dans la liste des option de notre champs \u00ab\u00a0select\u00a0\u00bb<\/p>\n<div class=\"codecolorer-container html4strict default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"html4strict codecolorer\">&lt;<a href=\"http:\/\/december.com\/html\/4\/element\/div.html\"><span class=\"kw2\">div<\/span><\/a> <span class=\"kw3\">class<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;form-group&quot;<\/span>&gt;<br \/>\n&nbsp; &nbsp; &lt;<a href=\"http:\/\/december.com\/html\/4\/element\/label.html\"><span class=\"kw2\">label<\/span><\/a> <span class=\"kw3\">for<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;instrument&quot;<\/span>&gt;Instrument&lt;<span class=\"sy0\">\/<\/span><a href=\"http:\/\/december.com\/html\/4\/element\/label.html\"><span class=\"kw2\">label<\/span><\/a>&gt;<br \/>\n&nbsp; &nbsp; &lt;<a href=\"http:\/\/december.com\/html\/4\/element\/select.html\"><span class=\"kw2\">select<\/span><\/a> <span class=\"kw3\">id<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;instrument&quot;<\/span> <span class=\"kw3\">class<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;form-control&quot;<\/span> <span class=\"kw3\">name<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;instrument&quot;<\/span>&gt;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;<a href=\"http:\/\/december.com\/html\/4\/element\/option.html\"><span class=\"kw2\">option<\/span><\/a> <span class=\"kw3\">value<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;1&quot;<\/span>&gt;accord\u00e9on&lt;<span class=\"sy0\">\/<\/span><a href=\"http:\/\/december.com\/html\/4\/element\/option.html\"><span class=\"kw2\">option<\/span><\/a>&gt;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;<a href=\"http:\/\/december.com\/html\/4\/element\/option.html\"><span class=\"kw2\">option<\/span><\/a> <span class=\"kw3\">value<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;2&quot;<\/span>&gt;bandon\u00e9on&lt;<span class=\"sy0\">\/<\/span><a href=\"http:\/\/december.com\/html\/4\/element\/option.html\"><span class=\"kw2\">option<\/span><\/a>&gt;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;<a href=\"http:\/\/december.com\/html\/4\/element\/option.html\"><span class=\"kw2\">option<\/span><\/a> <span class=\"kw3\">value<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;3&quot;<\/span>&gt;banjo&lt;<span class=\"sy0\">\/<\/span><a href=\"http:\/\/december.com\/html\/4\/element\/option.html\"><span class=\"kw2\">option<\/span><\/a>&gt;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;<a href=\"http:\/\/december.com\/html\/4\/element\/option.html\"><span class=\"kw2\">option<\/span><\/a> <span class=\"kw3\">value<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;4&quot;<\/span>&gt;batterie&lt;<span class=\"sy0\">\/<\/span><a href=\"http:\/\/december.com\/html\/4\/element\/option.html\"><span class=\"kw2\">option<\/span><\/a>&gt;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;!-- Toutes les autres options --&gt;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;!-- Notre option de valeur <span class=\"st0\">&quot;autre&quot;<\/span> --&gt;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;<a href=\"http:\/\/december.com\/html\/4\/element\/option.html\"><span class=\"kw2\">option<\/span><\/a> <span class=\"kw3\">value<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;autre&quot;<\/span>&gt;Autre instrument&lt;<span class=\"sy0\">\/<\/span><a href=\"http:\/\/december.com\/html\/4\/element\/option.html\"><span class=\"kw2\">option<\/span><\/a>&gt;<br \/>\n&nbsp; &nbsp; &lt;<span class=\"sy0\">\/<\/span><a href=\"http:\/\/december.com\/html\/4\/element\/select.html\"><span class=\"kw2\">select<\/span><\/a>&gt;<br \/>\n&lt;<span class=\"sy0\">\/<\/span><a href=\"http:\/\/december.com\/html\/4\/element\/div.html\"><span class=\"kw2\">div<\/span><\/a>&gt;<br \/>\n&lt;!-- les autre champs \u00e0 afficher<span class=\"sy0\">\/<\/span>masquer selon le choix dans la liste pr\u00e9c\u00e9dente --&gt;<br \/>\n&lt;<a href=\"http:\/\/december.com\/html\/4\/element\/div.html\"><span class=\"kw2\">div<\/span><\/a> <span class=\"kw3\">class<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;form-group&quot;<\/span>&gt;<br \/>\n&nbsp; &nbsp; &lt;<a href=\"http:\/\/december.com\/html\/4\/element\/label.html\"><span class=\"kw2\">label<\/span><\/a> <span class=\"kw3\">for<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;otherInstrumentCategory&quot;<\/span>&gt;Cat\u00e9gorie de l<span class=\"st0\">'instrument&lt;\/label&gt;<br \/>\n&nbsp; &nbsp; &lt;select class=&quot;form-control&quot; id=&quot;otherInstrumentCategory&quot; name=&quot;otherInstrumentCategory&quot; &gt;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;option value=&quot;1&quot;&gt;Instrument \u00e0 corde&lt;\/option&gt;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;option value=&quot;2&quot;&gt;Percussion&lt;\/option&gt;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;option value=&quot;4&quot;&gt;Instrument \u00e0 vent&lt;\/option&gt;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;option value=&quot;5&quot;&gt;MAO&lt;\/option&gt;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;option value=&quot;6&quot;&gt;Autre&lt;\/option&gt;<br \/>\n&nbsp; &nbsp; &lt;\/select&gt;<br \/>\n&lt;\/div&gt;<br \/>\n&lt;div class=&quot;form-group&quot;&gt;<br \/>\n&nbsp; &nbsp; &lt;label for=&quot;otherInstrument&quot;&gt;Nom de l'<\/span>instrument&lt;<span class=\"sy0\">\/<\/span><a href=\"http:\/\/december.com\/html\/4\/element\/label.html\"><span class=\"kw2\">label<\/span><\/a>&gt;<br \/>\n&nbsp; &nbsp; &lt;<a href=\"http:\/\/december.com\/html\/4\/element\/input.html\"><span class=\"kw2\">input<\/span><\/a> <span class=\"kw3\">type<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;text&quot;<\/span> <span class=\"kw3\">id<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;otherInstrument&quot;<\/span> <span class=\"kw3\">name<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;otherInstrument&quot;<\/span> <span class=\"kw3\">class<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;form-control&quot;<\/span>&gt;<br \/>\n&lt;<span class=\"sy0\">\/<\/span><a href=\"http:\/\/december.com\/html\/4\/element\/div.html\"><span class=\"kw2\">div<\/span><\/a>&gt;<\/div><\/div>\n<p>Et pour finir notre javascript qui va permettre de toujours laisser en place l&rsquo;option \u00ab\u00a0autre instrument\u00a0\u00bb malgr\u00e9 une recherche, ainsi que d&rsquo;afficher ou masquer les champs suppl\u00e9mentaires. Rien de bien complexe, suivez les commentaires dans le code pour comprendre.<\/p>\n<div class=\"codecolorer-container javascript default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"javascript codecolorer\">$<span class=\"br0\">&#40;<\/span>document<span class=\"br0\">&#41;<\/span>.<span class=\"me1\">ready<\/span><span class=\"br0\">&#40;<\/span><span class=\"kw1\">function<\/span> <span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"coMULTI\">\/*au changement du champs instrument on met \u00e0 jour l'affichage des champs*\/<\/span><br \/>\n&nbsp; &nbsp; $<span class=\"br0\">&#40;<\/span>document<span class=\"br0\">&#41;<\/span>.<span class=\"me1\">on<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;change&quot;<\/span><span class=\"sy0\">,<\/span> <span class=\"st0\">&quot;#instrument&quot;<\/span><span class=\"sy0\">,<\/span> <span class=\"kw1\">function<\/span> <span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; updateFields<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"coMULTI\">\/*initialisation du champs instrument, pour l'utilisation du plugin select2 avec un filtre de recherche particulier*\/<\/span><br \/>\n&nbsp; &nbsp; $<span class=\"br0\">&#40;<\/span><span class=\"st0\">'select#instrument'<\/span><span class=\"br0\">&#41;<\/span>.<span class=\"me1\">select2<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#123;<\/span>matcher<span class=\"sy0\">:<\/span> select2matchCustomInstrument<span class=\"br0\">&#125;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"coMULTI\">\/*initialisation de 'laffichage des champs*\/<\/span><br \/>\n&nbsp; &nbsp; updateFields<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<span class=\"coMULTI\">\/*On va utiliser le syst\u00e8me de recherche classique du plugin select2*\/<\/span><br \/>\n<span class=\"kw1\">var<\/span> defaultMatcher <span class=\"sy0\">=<\/span> $.<span class=\"me1\">fn<\/span>.<span class=\"me1\">select2<\/span>.<span class=\"me1\">defaults<\/span>.<span class=\"me1\">defaults<\/span>.<span class=\"me1\">matcher<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw1\">function<\/span> select2matchCustomInstrument<span class=\"br0\">&#40;<\/span>params<span class=\"sy0\">,<\/span> data<span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"coMULTI\">\/* Si l'option est &quot;Autre&quot; on l'affiche toujours *\/<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span>params.<span class=\"me1\">term<\/span> <span class=\"sy0\">&amp;&amp;<\/span> data.<span class=\"me1\">id<\/span> <span class=\"sy0\">==<\/span> <span class=\"st0\">&quot;autre&quot;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">return<\/span> data<span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"coMULTI\">\/* Sinon utilisation de la recherche classique *\/<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">return<\/span> defaultMatcher<span class=\"br0\">&#40;<\/span>params<span class=\"sy0\">,<\/span> data<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\n<span class=\"coMULTI\">\/*la fonction de mise \u00e0 jour de l'affichage des champs*\/<\/span><br \/>\n<span class=\"kw1\">function<\/span> updateFields<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"coMULTI\">\/* Si la valeur du champ instrument est &quot;autre&quot; on affiche les champs suppl\u00e9mentaire, sinon on les masque *\/<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span>$<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;#instrument&quot;<\/span><span class=\"br0\">&#41;<\/span>.<span class=\"me1\">val<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"sy0\">==<\/span> <span class=\"st0\">&quot;autre&quot;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; $<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;#otherInstrumentCategory&quot;<\/span><span class=\"br0\">&#41;<\/span>.<span class=\"me1\">parents<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;.form-group&quot;<\/span><span class=\"br0\">&#41;<\/span>.<span class=\"me1\">css<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;display&quot;<\/span><span class=\"sy0\">,<\/span> <span class=\"st0\">&quot;block&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; $<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;#otherInstrument&quot;<\/span><span class=\"br0\">&#41;<\/span>.<span class=\"me1\">parents<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;.form-group&quot;<\/span><span class=\"br0\">&#41;<\/span>.<span class=\"me1\">css<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;display&quot;<\/span><span class=\"sy0\">,<\/span> <span class=\"st0\">&quot;block&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span> <span class=\"kw1\">else<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; $<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;#otherInstrumentCategory&quot;<\/span><span class=\"br0\">&#41;<\/span>.<span class=\"me1\">parents<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;.form-group&quot;<\/span><span class=\"br0\">&#41;<\/span>.<span class=\"me1\">css<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;display&quot;<\/span><span class=\"sy0\">,<\/span> <span class=\"st0\">&quot;none&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; $<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;#otherInstrument&quot;<\/span><span class=\"br0\">&#41;<\/span>.<span class=\"me1\">parents<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;.form-group&quot;<\/span><span class=\"br0\">&#41;<\/span>.<span class=\"me1\">css<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;display&quot;<\/span><span class=\"sy0\">,<\/span> <span class=\"st0\">&quot;none&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>Ce plugin peux \u00eatre adapt\u00e9 de fa\u00e7on diverse, comme par exemple pour <a href=\"https:\/\/numa-bord.com\/miniblog\/jquery-creer-un-font-picker-avec-le-plugin-select2\/\">g\u00e9n\u00e9rer un font-picker<\/a> comme vu dans un article pr\u00e9c\u00e9dent.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Le plugin jQuery \u00ab\u00a0Select2\u00a0\u00bb permet d\u2019am\u00e9liorer les champs de type \u00ab\u00a0select\u00a0\u00bb (liste d\u00e9roulante). Une de ces principales fonctionnalit\u00e9 et d&rsquo;int\u00e9grer un champs de recherche \u00e0 la liste. Tr\u00e8s pratique pour retrouver et s\u00e9lectionner un \u00e9l\u00e9ments dans une longue liste. Nous allons voir ci-dessous comment mettre en place une astuce afin qu&rsquo;une option de la liste [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2,11,12],"tags":[],"class_list":["post-185","post","type-post","status-publish","format-standard","hentry","category-developpement","category-javascript","category-jquery"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>jQuery : rendre l&#039;option d&#039;un select permanente avec le plugin &quot;select2&quot; - Pense b\u00eate d&#039;un d\u00e9veloppeur web<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/numa-bord.com\/miniblog\/jquery-rendre-loption-dun-select-permanente-plugin-select2\/\" \/>\n<meta property=\"og:locale\" content=\"fr_FR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"jQuery : rendre l&#039;option d&#039;un select permanente avec le plugin &quot;select2&quot; - Pense b\u00eate d&#039;un d\u00e9veloppeur web\" \/>\n<meta property=\"og:description\" content=\"Le plugin jQuery \u00ab\u00a0Select2\u00a0\u00bb permet d\u2019am\u00e9liorer les champs de type \u00ab\u00a0select\u00a0\u00bb (liste d\u00e9roulante). Une de ces principales fonctionnalit\u00e9 et d&rsquo;int\u00e9grer un champs de recherche \u00e0 la liste. Tr\u00e8s pratique pour retrouver et s\u00e9lectionner un \u00e9l\u00e9ments dans une longue liste. Nous allons voir ci-dessous comment mettre en place une astuce afin qu&rsquo;une option de la liste [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/numa-bord.com\/miniblog\/jquery-rendre-loption-dun-select-permanente-plugin-select2\/\" \/>\n<meta property=\"og:site_name\" content=\"Pense b\u00eate d&#039;un d\u00e9veloppeur web\" \/>\n<meta property=\"article:published_time\" content=\"2017-10-16T07:27:31+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/numa-bord.com\/miniblog\/wp-content\/uploads\/2017\/10\/select2-option-permanente.jpg\" \/>\n<meta name=\"author\" content=\"Numa\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u00c9crit par\" \/>\n\t<meta name=\"twitter:data1\" content=\"Numa\" \/>\n\t<meta name=\"twitter:label2\" content=\"Dur\u00e9e de lecture estim\u00e9e\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/numa-bord.com\/miniblog\/jquery-rendre-loption-dun-select-permanente-plugin-select2\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/numa-bord.com\/miniblog\/jquery-rendre-loption-dun-select-permanente-plugin-select2\/\"},\"author\":{\"name\":\"Numa\",\"@id\":\"https:\/\/numa-bord.com\/miniblog\/#\/schema\/person\/f9d00acd1703f17e5a6895283eb46a7e\"},\"headline\":\"jQuery : rendre l&rsquo;option d&rsquo;un select permanente avec le plugin \u00ab\u00a0select2\u00a0\u00bb\",\"datePublished\":\"2017-10-16T07:27:31+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/numa-bord.com\/miniblog\/jquery-rendre-loption-dun-select-permanente-plugin-select2\/\"},\"wordCount\":472,\"commentCount\":2,\"image\":{\"@id\":\"https:\/\/numa-bord.com\/miniblog\/jquery-rendre-loption-dun-select-permanente-plugin-select2\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/numa-bord.com\/miniblog\/wp-content\/uploads\/2017\/10\/select2-option-permanente.jpg\",\"articleSection\":[\"D\u00e9veloppement\",\"Javascript\",\"jQuery\"],\"inLanguage\":\"fr-FR\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/numa-bord.com\/miniblog\/jquery-rendre-loption-dun-select-permanente-plugin-select2\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/numa-bord.com\/miniblog\/jquery-rendre-loption-dun-select-permanente-plugin-select2\/\",\"url\":\"https:\/\/numa-bord.com\/miniblog\/jquery-rendre-loption-dun-select-permanente-plugin-select2\/\",\"name\":\"jQuery : rendre l'option d'un select permanente avec le plugin \\\"select2\\\" - Pense b\u00eate d&#039;un d\u00e9veloppeur web\",\"isPartOf\":{\"@id\":\"https:\/\/numa-bord.com\/miniblog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/numa-bord.com\/miniblog\/jquery-rendre-loption-dun-select-permanente-plugin-select2\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/numa-bord.com\/miniblog\/jquery-rendre-loption-dun-select-permanente-plugin-select2\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/numa-bord.com\/miniblog\/wp-content\/uploads\/2017\/10\/select2-option-permanente.jpg\",\"datePublished\":\"2017-10-16T07:27:31+00:00\",\"author\":{\"@id\":\"https:\/\/numa-bord.com\/miniblog\/#\/schema\/person\/f9d00acd1703f17e5a6895283eb46a7e\"},\"breadcrumb\":{\"@id\":\"https:\/\/numa-bord.com\/miniblog\/jquery-rendre-loption-dun-select-permanente-plugin-select2\/#breadcrumb\"},\"inLanguage\":\"fr-FR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/numa-bord.com\/miniblog\/jquery-rendre-loption-dun-select-permanente-plugin-select2\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"fr-FR\",\"@id\":\"https:\/\/numa-bord.com\/miniblog\/jquery-rendre-loption-dun-select-permanente-plugin-select2\/#primaryimage\",\"url\":\"https:\/\/numa-bord.com\/miniblog\/wp-content\/uploads\/2017\/10\/select2-option-permanente.jpg\",\"contentUrl\":\"https:\/\/numa-bord.com\/miniblog\/wp-content\/uploads\/2017\/10\/select2-option-permanente.jpg\",\"width\":1156,\"height\":226},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/numa-bord.com\/miniblog\/jquery-rendre-loption-dun-select-permanente-plugin-select2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Accueil\",\"item\":\"https:\/\/numa-bord.com\/miniblog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"jQuery : rendre l&rsquo;option d&rsquo;un select permanente avec le plugin \u00ab\u00a0select2\u00a0\u00bb\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/numa-bord.com\/miniblog\/#website\",\"url\":\"https:\/\/numa-bord.com\/miniblog\/\",\"name\":\"Pense b\u00eate d&#039;un d\u00e9veloppeur web\",\"description\":\"(php, javascript, Symfony, Wordpress....)\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/numa-bord.com\/miniblog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"fr-FR\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/numa-bord.com\/miniblog\/#\/schema\/person\/f9d00acd1703f17e5a6895283eb46a7e\",\"name\":\"Numa\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"fr-FR\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/f21d1af4658a7106211915940584534c1e0b3eef3f12eb67a697686cad70b64a?s=96&d=mm&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/f21d1af4658a7106211915940584534c1e0b3eef3f12eb67a697686cad70b64a?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/f21d1af4658a7106211915940584534c1e0b3eef3f12eb67a697686cad70b64a?s=96&d=mm&r=g\",\"caption\":\"Numa\"},\"url\":\"https:\/\/numa-bord.com\/miniblog\/author\/negonner\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"jQuery : rendre l'option d'un select permanente avec le plugin \"select2\" - Pense b\u00eate d&#039;un d\u00e9veloppeur web","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/numa-bord.com\/miniblog\/jquery-rendre-loption-dun-select-permanente-plugin-select2\/","og_locale":"fr_FR","og_type":"article","og_title":"jQuery : rendre l'option d'un select permanente avec le plugin \"select2\" - Pense b\u00eate d&#039;un d\u00e9veloppeur web","og_description":"Le plugin jQuery \u00ab\u00a0Select2\u00a0\u00bb permet d\u2019am\u00e9liorer les champs de type \u00ab\u00a0select\u00a0\u00bb (liste d\u00e9roulante). Une de ces principales fonctionnalit\u00e9 et d&rsquo;int\u00e9grer un champs de recherche \u00e0 la liste. Tr\u00e8s pratique pour retrouver et s\u00e9lectionner un \u00e9l\u00e9ments dans une longue liste. Nous allons voir ci-dessous comment mettre en place une astuce afin qu&rsquo;une option de la liste [&hellip;]","og_url":"https:\/\/numa-bord.com\/miniblog\/jquery-rendre-loption-dun-select-permanente-plugin-select2\/","og_site_name":"Pense b\u00eate d&#039;un d\u00e9veloppeur web","article_published_time":"2017-10-16T07:27:31+00:00","og_image":[{"url":"https:\/\/numa-bord.com\/miniblog\/wp-content\/uploads\/2017\/10\/select2-option-permanente.jpg","type":"","width":"","height":""}],"author":"Numa","twitter_card":"summary_large_image","twitter_misc":{"\u00c9crit par":"Numa","Dur\u00e9e de lecture estim\u00e9e":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/numa-bord.com\/miniblog\/jquery-rendre-loption-dun-select-permanente-plugin-select2\/#article","isPartOf":{"@id":"https:\/\/numa-bord.com\/miniblog\/jquery-rendre-loption-dun-select-permanente-plugin-select2\/"},"author":{"name":"Numa","@id":"https:\/\/numa-bord.com\/miniblog\/#\/schema\/person\/f9d00acd1703f17e5a6895283eb46a7e"},"headline":"jQuery : rendre l&rsquo;option d&rsquo;un select permanente avec le plugin \u00ab\u00a0select2\u00a0\u00bb","datePublished":"2017-10-16T07:27:31+00:00","mainEntityOfPage":{"@id":"https:\/\/numa-bord.com\/miniblog\/jquery-rendre-loption-dun-select-permanente-plugin-select2\/"},"wordCount":472,"commentCount":2,"image":{"@id":"https:\/\/numa-bord.com\/miniblog\/jquery-rendre-loption-dun-select-permanente-plugin-select2\/#primaryimage"},"thumbnailUrl":"https:\/\/numa-bord.com\/miniblog\/wp-content\/uploads\/2017\/10\/select2-option-permanente.jpg","articleSection":["D\u00e9veloppement","Javascript","jQuery"],"inLanguage":"fr-FR","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/numa-bord.com\/miniblog\/jquery-rendre-loption-dun-select-permanente-plugin-select2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/numa-bord.com\/miniblog\/jquery-rendre-loption-dun-select-permanente-plugin-select2\/","url":"https:\/\/numa-bord.com\/miniblog\/jquery-rendre-loption-dun-select-permanente-plugin-select2\/","name":"jQuery : rendre l'option d'un select permanente avec le plugin \"select2\" - Pense b\u00eate d&#039;un d\u00e9veloppeur web","isPartOf":{"@id":"https:\/\/numa-bord.com\/miniblog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/numa-bord.com\/miniblog\/jquery-rendre-loption-dun-select-permanente-plugin-select2\/#primaryimage"},"image":{"@id":"https:\/\/numa-bord.com\/miniblog\/jquery-rendre-loption-dun-select-permanente-plugin-select2\/#primaryimage"},"thumbnailUrl":"https:\/\/numa-bord.com\/miniblog\/wp-content\/uploads\/2017\/10\/select2-option-permanente.jpg","datePublished":"2017-10-16T07:27:31+00:00","author":{"@id":"https:\/\/numa-bord.com\/miniblog\/#\/schema\/person\/f9d00acd1703f17e5a6895283eb46a7e"},"breadcrumb":{"@id":"https:\/\/numa-bord.com\/miniblog\/jquery-rendre-loption-dun-select-permanente-plugin-select2\/#breadcrumb"},"inLanguage":"fr-FR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/numa-bord.com\/miniblog\/jquery-rendre-loption-dun-select-permanente-plugin-select2\/"]}]},{"@type":"ImageObject","inLanguage":"fr-FR","@id":"https:\/\/numa-bord.com\/miniblog\/jquery-rendre-loption-dun-select-permanente-plugin-select2\/#primaryimage","url":"https:\/\/numa-bord.com\/miniblog\/wp-content\/uploads\/2017\/10\/select2-option-permanente.jpg","contentUrl":"https:\/\/numa-bord.com\/miniblog\/wp-content\/uploads\/2017\/10\/select2-option-permanente.jpg","width":1156,"height":226},{"@type":"BreadcrumbList","@id":"https:\/\/numa-bord.com\/miniblog\/jquery-rendre-loption-dun-select-permanente-plugin-select2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Accueil","item":"https:\/\/numa-bord.com\/miniblog\/"},{"@type":"ListItem","position":2,"name":"jQuery : rendre l&rsquo;option d&rsquo;un select permanente avec le plugin \u00ab\u00a0select2\u00a0\u00bb"}]},{"@type":"WebSite","@id":"https:\/\/numa-bord.com\/miniblog\/#website","url":"https:\/\/numa-bord.com\/miniblog\/","name":"Pense b\u00eate d&#039;un d\u00e9veloppeur web","description":"(php, javascript, Symfony, Wordpress....)","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/numa-bord.com\/miniblog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"fr-FR"},{"@type":"Person","@id":"https:\/\/numa-bord.com\/miniblog\/#\/schema\/person\/f9d00acd1703f17e5a6895283eb46a7e","name":"Numa","image":{"@type":"ImageObject","inLanguage":"fr-FR","@id":"https:\/\/secure.gravatar.com\/avatar\/f21d1af4658a7106211915940584534c1e0b3eef3f12eb67a697686cad70b64a?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/f21d1af4658a7106211915940584534c1e0b3eef3f12eb67a697686cad70b64a?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f21d1af4658a7106211915940584534c1e0b3eef3f12eb67a697686cad70b64a?s=96&d=mm&r=g","caption":"Numa"},"url":"https:\/\/numa-bord.com\/miniblog\/author\/negonner\/"}]}},"_links":{"self":[{"href":"https:\/\/numa-bord.com\/miniblog\/wp-json\/wp\/v2\/posts\/185","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/numa-bord.com\/miniblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/numa-bord.com\/miniblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/numa-bord.com\/miniblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/numa-bord.com\/miniblog\/wp-json\/wp\/v2\/comments?post=185"}],"version-history":[{"count":4,"href":"https:\/\/numa-bord.com\/miniblog\/wp-json\/wp\/v2\/posts\/185\/revisions"}],"predecessor-version":[{"id":190,"href":"https:\/\/numa-bord.com\/miniblog\/wp-json\/wp\/v2\/posts\/185\/revisions\/190"}],"wp:attachment":[{"href":"https:\/\/numa-bord.com\/miniblog\/wp-json\/wp\/v2\/media?parent=185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/numa-bord.com\/miniblog\/wp-json\/wp\/v2\/categories?post=185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/numa-bord.com\/miniblog\/wp-json\/wp\/v2\/tags?post=185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}