{"id":7935,"date":"2021-08-14T06:49:46","date_gmt":"2021-08-14T06:49:46","guid":{"rendered":"https:\/\/www.scmgalaxy.com\/tutorials\/?p=7935"},"modified":"2021-08-14T06:49:49","modified_gmt":"2021-08-14T06:49:49","slug":"how-to-add-placeholder-to-select2-js","status":"publish","type":"post","link":"https:\/\/www.scmgalaxy.com\/tutorials\/how-to-add-placeholder-to-select2-js\/","title":{"rendered":"How to add placeholder to select2.js"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Single select placeholders<\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"829\" height=\"162\" src=\"https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2021\/08\/image-95.png\" alt=\"\" class=\"wp-image-7936\" srcset=\"https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2021\/08\/image-95.png 829w, https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2021\/08\/image-95-300x59.png 300w, https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2021\/08\/image-95-768x150.png 768w\" sizes=\"auto, (max-width: 829px) 100vw, 829px\" \/><\/figure>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;select class=&quot;js-example-placeholder-single js-states form-control&quot;&gt;\n  &amp;lt;option&gt;&amp;lt;\/option&gt;\n&amp;lt;\/select&gt;\n\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n$(&quot;.js-example-placeholder-single&quot;).select2({\n    placeholder: &quot;Select a state&quot;,\n    allowClear: true\n});\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Multi-select placeholders<\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"868\" height=\"112\" src=\"https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2021\/08\/image-96.png\" alt=\"\" class=\"wp-image-7937\" srcset=\"https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2021\/08\/image-96.png 868w, https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2021\/08\/image-96-300x39.png 300w, https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2021\/08\/image-96-768x99.png 768w\" sizes=\"auto, (max-width: 868px) 100vw, 868px\" \/><\/figure>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;select class=&quot;js-example-placeholder-multiple js-states form-control&quot; multiple=&quot;multiple&quot;&gt;&amp;lt;\/select&gt;\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n$(&quot;.js-example-placeholder-multiple&quot;).select2({\n    placeholder: &quot;Select a state&quot;\n});\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Using placeholders with AJAX<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Select2 supports placeholders for all configurations, including AJAX. You will still need to add in the empty\u00a0<code>&lt;option><\/code>\u00a0if you are using a single select.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When using Select2\u00a0<strong>in single-selection mode<\/strong>, the placeholder option will be passed through the\u00a0<code>templateSelection<\/code>\u00a0callback if specified. You can use some additional logic in this callback to check the\u00a0<code>id<\/code>\u00a0property and apply an alternative transformation to your placeholder option:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n$(&#039;select&#039;).select2({\n  templateSelection: function (data) {\n    if (data.id === &#039;&#039;) { \/\/ adjust for custom placeholder values\n      return &#039;Custom styled placeholder text&#039;;\n    }\n\n    return data.text;\n  }\n});\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>Single select placeholders Multi-select placeholders Using placeholders with AJAX Select2 supports placeholders for all configurations, including AJAX. You will still [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[16],"tags":[5095,6310],"class_list":["post-7935","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-javascript","tag-select2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to add placeholder to select2.js - scmGalaxy<\/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:\/\/www.scmgalaxy.com\/tutorials\/how-to-add-placeholder-to-select2-js\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to add placeholder to select2.js - scmGalaxy\" \/>\n<meta property=\"og:description\" content=\"Single select placeholders Multi-select placeholders Using placeholders with AJAX Select2 supports placeholders for all configurations, including AJAX. You will still [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.scmgalaxy.com\/tutorials\/how-to-add-placeholder-to-select2-js\/\" \/>\n<meta property=\"og:site_name\" content=\"scmGalaxy\" \/>\n<meta property=\"article:published_time\" content=\"2021-08-14T06:49:46+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-08-14T06:49:49+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2021\/08\/image-95.png\" \/>\n<meta name=\"author\" content=\"Amardeep Dubey\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Amardeep Dubey\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-to-add-placeholder-to-select2-js\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-to-add-placeholder-to-select2-js\\\/\"},\"author\":{\"name\":\"Amardeep Dubey\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#\\\/schema\\\/person\\\/097cf9059af187b39521d8641650ddda\"},\"headline\":\"How to add placeholder to select2.js\",\"datePublished\":\"2021-08-14T06:49:46+00:00\",\"dateModified\":\"2021-08-14T06:49:49+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-to-add-placeholder-to-select2-js\\\/\"},\"wordCount\":79,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-to-add-placeholder-to-select2-js\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/wp-content\\\/uploads\\\/2021\\\/08\\\/image-95.png\",\"keywords\":[\"Javascript\",\"select2\"],\"articleSection\":[\"JavaScript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-to-add-placeholder-to-select2-js\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-to-add-placeholder-to-select2-js\\\/\",\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-to-add-placeholder-to-select2-js\\\/\",\"name\":\"How to add placeholder to select2.js - scmGalaxy\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-to-add-placeholder-to-select2-js\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-to-add-placeholder-to-select2-js\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/wp-content\\\/uploads\\\/2021\\\/08\\\/image-95.png\",\"datePublished\":\"2021-08-14T06:49:46+00:00\",\"dateModified\":\"2021-08-14T06:49:49+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#\\\/schema\\\/person\\\/097cf9059af187b39521d8641650ddda\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-to-add-placeholder-to-select2-js\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-to-add-placeholder-to-select2-js\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-to-add-placeholder-to-select2-js\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/wp-content\\\/uploads\\\/2021\\\/08\\\/image-95.png\",\"contentUrl\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/wp-content\\\/uploads\\\/2021\\\/08\\\/image-95.png\",\"width\":829,\"height\":162},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-to-add-placeholder-to-select2-js\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to add placeholder to select2.js\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#website\",\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/\",\"name\":\"scmGalaxy\",\"description\":\"A Platform for Software Engineers\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#\\\/schema\\\/person\\\/097cf9059af187b39521d8641650ddda\",\"name\":\"Amardeep Dubey\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/79b318f73f1ee84d7678ea32f5af8984012529c7a67c2deb7e3030dc8c6114ba?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/79b318f73f1ee84d7678ea32f5af8984012529c7a67c2deb7e3030dc8c6114ba?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/79b318f73f1ee84d7678ea32f5af8984012529c7a67c2deb7e3030dc8c6114ba?s=96&d=mm&r=g\",\"caption\":\"Amardeep Dubey\"},\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/author\\\/amardeep\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to add placeholder to select2.js - scmGalaxy","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:\/\/www.scmgalaxy.com\/tutorials\/how-to-add-placeholder-to-select2-js\/","og_locale":"en_US","og_type":"article","og_title":"How to add placeholder to select2.js - scmGalaxy","og_description":"Single select placeholders Multi-select placeholders Using placeholders with AJAX Select2 supports placeholders for all configurations, including AJAX. You will still [&hellip;]","og_url":"https:\/\/www.scmgalaxy.com\/tutorials\/how-to-add-placeholder-to-select2-js\/","og_site_name":"scmGalaxy","article_published_time":"2021-08-14T06:49:46+00:00","article_modified_time":"2021-08-14T06:49:49+00:00","og_image":[{"url":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2021\/08\/image-95.png","type":"","width":"","height":""}],"author":"Amardeep Dubey","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Amardeep Dubey","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/how-to-add-placeholder-to-select2-js\/#article","isPartOf":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/how-to-add-placeholder-to-select2-js\/"},"author":{"name":"Amardeep Dubey","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#\/schema\/person\/097cf9059af187b39521d8641650ddda"},"headline":"How to add placeholder to select2.js","datePublished":"2021-08-14T06:49:46+00:00","dateModified":"2021-08-14T06:49:49+00:00","mainEntityOfPage":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/how-to-add-placeholder-to-select2-js\/"},"wordCount":79,"commentCount":0,"image":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/how-to-add-placeholder-to-select2-js\/#primaryimage"},"thumbnailUrl":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2021\/08\/image-95.png","keywords":["Javascript","select2"],"articleSection":["JavaScript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.scmgalaxy.com\/tutorials\/how-to-add-placeholder-to-select2-js\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/how-to-add-placeholder-to-select2-js\/","url":"https:\/\/www.scmgalaxy.com\/tutorials\/how-to-add-placeholder-to-select2-js\/","name":"How to add placeholder to select2.js - scmGalaxy","isPartOf":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/how-to-add-placeholder-to-select2-js\/#primaryimage"},"image":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/how-to-add-placeholder-to-select2-js\/#primaryimage"},"thumbnailUrl":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2021\/08\/image-95.png","datePublished":"2021-08-14T06:49:46+00:00","dateModified":"2021-08-14T06:49:49+00:00","author":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#\/schema\/person\/097cf9059af187b39521d8641650ddda"},"breadcrumb":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/how-to-add-placeholder-to-select2-js\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.scmgalaxy.com\/tutorials\/how-to-add-placeholder-to-select2-js\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/how-to-add-placeholder-to-select2-js\/#primaryimage","url":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2021\/08\/image-95.png","contentUrl":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2021\/08\/image-95.png","width":829,"height":162},{"@type":"BreadcrumbList","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/how-to-add-placeholder-to-select2-js\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.scmgalaxy.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"How to add placeholder to select2.js"}]},{"@type":"WebSite","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#website","url":"https:\/\/www.scmgalaxy.com\/tutorials\/","name":"scmGalaxy","description":"A Platform for Software Engineers","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.scmgalaxy.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#\/schema\/person\/097cf9059af187b39521d8641650ddda","name":"Amardeep Dubey","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/79b318f73f1ee84d7678ea32f5af8984012529c7a67c2deb7e3030dc8c6114ba?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/79b318f73f1ee84d7678ea32f5af8984012529c7a67c2deb7e3030dc8c6114ba?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/79b318f73f1ee84d7678ea32f5af8984012529c7a67c2deb7e3030dc8c6114ba?s=96&d=mm&r=g","caption":"Amardeep Dubey"},"url":"https:\/\/www.scmgalaxy.com\/tutorials\/author\/amardeep\/"}]}},"_links":{"self":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts\/7935","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/comments?post=7935"}],"version-history":[{"count":1,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts\/7935\/revisions"}],"predecessor-version":[{"id":7938,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts\/7935\/revisions\/7938"}],"wp:attachment":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/media?parent=7935"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/categories?post=7935"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/tags?post=7935"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}