{"id":305825,"date":"2021-09-16T19:56:59","date_gmt":"2021-09-16T19:56:59","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=305825"},"modified":"2024-07-25T07:00:40","modified_gmt":"2024-07-25T07:00:40","slug":"create-dropdown-in-custom-form-using-select2-js-in-magento-2","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/create-dropdown-in-custom-form-using-select2-js-in-magento-2\/","title":{"rendered":"Create Dropdown in custom form using select2 js in Magento 2"},"content":{"rendered":"\n<p>In this blog, we are going to learn how we can create a dropdown or select input type field in a form with dynamic options with the search features.<\/p>\n\n\n\n<p>In Magento2, we manage a large number of products and customers, and other data.<br>So, if we are creating a form and we have the need to add a dropdown or select input type, then using select2 js and adding options dynamically will improve our form&#8217;s <a href=\"https:\/\/webkul.com\/blog\/magento2-defer-js-webp-ttfb\/\">loading performance<\/a>.<\/p>\n\n\n\n<p>Please follow the below steps to add dropdown using select2 js:<br><br>1. <a href=\"https:\/\/github.com\/select2\/select2\/tags\" target=\"_blank\" rel=\"noreferrer noopener\">Download the select2 js file<\/a>.<br><br>2. Create routename_controller_actionname.xml file inside Vendor\/CustomModule\/view\/frontend\/layout\/ directory.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?xml version=&quot;1.0&quot;?&gt;\n&lt;!--\n\/**\n * Vendor CustomModule\n *\n * @category Webkul\n * @package  Vendor_CustomModule\n * @author   Khushboo Sahu\n *\/\n--&gt;\n&lt;page xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; layout=&quot;1column&quot; xsi:noNamespaceSchemaLocation=&quot;urn:magento:framework:View\/Layout\/etc\/page_configuration.xsd&quot;&gt;\n    &lt;head&gt;\n        &lt;css src=&quot;Vendor_CustomModule::css\/select2.css&quot; rel=&quot;stylesheet&quot; type=&quot;text\/css&quot; \/&gt;\n    &lt;\/head&gt;\n    &lt;body&gt;\n        &lt;referenceContainer name=&quot;content&quot;&gt;\n            &lt;block class=&quot;Magento\\Framework\\View\\Element\\Template&quot; \n                name=&quot;select_demo_index&quot; \n                template=&quot;Vendor_CustomModule::demo.phtml&quot; \n                cacheable= &quot;false&quot;\/&gt;\n        &lt;\/referenceContainer&gt;\n    &lt;\/body&gt;\n&lt;\/page&gt;<\/pre>\n\n\n\n<p>3. Create demo.phtml file inside app\/code\/Vendor\/CustomModule\/view\/frontend\/templates\/ directory.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php \n$requestUrl = $block-&gt;getUrl(&#039;routename\/controller\/actionanme&#039;); \/\/to fetch customer account list\n?&gt;\n&lt;select class=&quot;select2&quot; id=&quot;wk-customers&quot; style=&quot;width:40%&quot;&gt;\n    &lt;option value=&quot;&quot;&gt;Select Customer...&lt;\/option&gt;\n&lt;\/select&gt;\n&lt;br\/&gt;&lt;br\/&gt;\n&lt;button id=&quot;getvaluebtn&quot; title=&quot;Save&quot; type=&quot;button&quot; class=&quot;action-default scalable action-save action-secondary&quot;&gt;\n    &lt;span&gt;Get Selected Option Value&lt;\/span&gt;\n&lt;\/button&gt;\n            \n&lt;script type=&quot;text\/javascript&quot;&gt;\n    require(&#091;\n        &quot;jquery&quot;,\n        &quot;Vendor_CustomModule\/js\/select2&quot;\n    ], function($)  {\n        $(&#039;#wk-customers&#039;).select2({\n            ajax: {\n                url: &quot;&lt;?= \/* @noEscape *\/ $requestUrl ?&gt;&quot;+&quot;?isAjax=true&quot;,\n                delay: 250,\n                type: &quot;GET&quot;,\n                data: function (params) {\n                    return {\n                        q: params.term,\n                        page: params.page\n                    };\n                },\n                error: function (response) {\n                    $(&quot;.messages&quot;).css(&quot;display&quot;, &quot;none&quot;);\n                },\n                processResults: function (data, params) {\n                    params.page = params.page || 1;\n\n                    return {\n                        results: data.results,\n                        pagination: {\n                            more: (params.page * 1) &lt; data.noOfPages\n                        }\n                    };\n                },\n                cache: true\n            },\n            placeholder: &#039;Choose Customer...&#039;,\n            theme: &quot;classic&quot;,\n            templateSelection: formatListSelection\n        });\n\n        function formatListSelection (item) {\n            return item.text;\n        }\n\n        $(&quot;body&quot;).on(&quot;click&quot;, &quot;#getvaluebtn&quot;, function () {\n            var newVal  = $(&quot;#wk-customers&quot;).val();\n            alert(newVal);\n        });\n    });\n&lt;\/script&gt;<\/pre>\n\n\n\n<p>4. Create style2.css file inside app\/code\/Vendor\/CustomModule\/view\/frontend\/web\/css\/ directory. And Put style2.js file inside app\/code\/Vendor\/CustomModule\/view\/frontend\/web\/js\/ directory.<br><br>5. Create Index.php file inside app\/code\/Vendor\/CustomModule\/Controller\/Index\/ directory.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\n\/**\n * Webkul Software.\n *\n * @category  Webkul\n * @package   Vendor_CustomModule\n * @author    Webkul\n * @copyright Copyright (c) Webkul Software Private Limited (https:\/\/webkul.com)\n * @license   https:\/\/store.webkul.com\/license.html\n *\/\nnamespace Vendor\\CustomModule\\Controller\\Index;\n\nuse Magento\\Framework\\App\\Action\\Action;\nuse Magento\\Framework\\App\\Action\\Context;\nuse Magento\\Framework\\View\\Result\\PageFactory;\n\nclass Index extends Action\n{\n    \/**\n     * @var PageFactory\n     *\/\n    protected $_resultPageFactory;\n\n    \/**\n     * initialization\n     *\n     * @param Context $context\n     * @param PageFactory $resultPageFactory\n     *\/\n    public function __construct(\n        Context $context,\n        PageFactory $resultPageFactory\n    ) {\n        $this-&gt;_resultPageFactory = $resultPageFactory;\n        parent::__construct($context);\n    }\n    \n    public function execute()\n    {\n        $resultPage = $this-&gt;_resultPageFactory-&gt;create();\n        $resultPage-&gt;getConfig()-&gt;getTitle()-&gt;set(__(&quot;Select2 Demo&quot;));\n        \n        return $resultPage;\n    }\n}<\/pre>\n\n\n\n<p>6. Now, Create CustomerAccountList.php Controller inside app\/code\/Vendor\/CustomModule\/Controller\/Index\/ directory.<br>When we will execute this controller, it will return response data in JSON format, in which the &#8220;results&#8221; key contains the customers&#8217; list.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\n\/**\n* Webkul Software.\n*\n* @category  Webkul\n* @package   Vendor_CustomModule\n* @author    Webkul\n* @copyright Webkul Software Private Limited (https:\/\/webkul.com)\n* @license   https:\/\/store.webkul.com\/license.html\n*\/\n\nnamespace Vendor\\CustomModule\\Controller\\Index;\n\nuse Magento\\Framework\\Controller\\ResultFactory;\nuse Magento\\Framework\\App\\Action\\Context;\n\nclass CustomerAccountList extends \\Magento\\Framework\\App\\Action\\Action\n{\n    \/**\n     * @var ResultFactory\n     *\/\n    protected $_resultFactory;\n\n    \/**\n     * @var array\n     *\/\n    protected $results = &#091;];\n\n    \/**\n     * @var int\n     *\/\n    private $resultsCount;\n\n    \/**\n     * Object initialization.\n     *\n     * @param Context $context\n     * @param \\Magento\\Customer\\Model\\CustomerFactory $customerFactory\n     * @param \\Magento\\Framework\\Controller\\ResultFactory $resultFactory\n     *\/\n    public function __construct(\n        Context $context,\n        \\Magento\\Customer\\Model\\CustomerFactory $customerFactory,\n        \\Magento\\Framework\\Controller\\ResultFactory $resultFactory\n    ) {\n        $this-&gt;_resultFactory  = $resultFactory;\n        $this-&gt;customerFactory = $customerFactory;\n        parent::__construct($context);\n    }\n\n    \/**\n     * Executes request and return json data\n     * @return json\n     *\/\n    public function execute()\n    {\n        $returnArray            = &#091;];\n        $returnArray&#091;&quot;success&quot;] = false;\n        $returnArray&#091;&quot;message&quot;] = &quot;&quot;;\n        $resultJson             = $this-&gt;_resultFactory-&gt;create(ResultFactory::TYPE_JSON);\n        $wholeData              = $this-&gt;getRequest()-&gt;getParams();\n\n        if ($wholeData) {\n            try {\n                $query       = &quot;&quot;;\n                $pageSize    = 10;\n                $pageNumber  = $wholeData&#091;&quot;page&quot;] ?? 1;\n                $query       = trim($wholeData&#091;&quot;q&quot;] ?? &quot;&quot;);\n                    \n                $customerList = &#091;];\n                $customerList = $this-&gt;getCustomers($query, $pageSize, $pageNumber);\n                        \n                $returnArray&#091;&quot;success&quot;]    = true;\n                $returnArray&#091;&quot;results&quot;]    = $customerList;\n                $returnArray&#091;&quot;totalCount&quot;] = $this-&gt;resultsCount;\n                $noOfPages = ($this-&gt;resultsCount &gt; 0)?$this-&gt;resultsCount\/$pageSize : 1;\n                $returnArray&#091;&quot;noOfPages&quot;]  = ceil($noOfPages);\n                    \n                $resultJson-&gt;setData($returnArray);\n                return $resultJson;\n            } catch (\\Exception $e) {\n                $returnArray&#091;&quot;message&quot;] = $e-&gt;getMessage();\n                $resultJson-&gt;setData($returnArray);\n                return $resultJson;\n            }\n        } else {\n            $returnArray&#091;&quot;message&quot;] = __(&quot;Invalid Request&quot;);\n            $resultJson-&gt;setData($returnArray);\n            return $resultJson;\n        }\n    }\n\n    \/**\n     * get customers\n     * @param string $query\n     * @param int $pageSize\n     * @param int $pageNumber\n     * @return array\n     *\/\n    public function getCustomers($query = &quot;&quot;, $pageSize = 20, $pageNumber = 1)\n    {\n        try {\n            $customers = $this-&gt;customerFactory-&gt;create()\n                -&gt;getCollection()\n                -&gt;addAttributeToSelect(&#039;*&#039;);\n                \n            if (!empty($query)) {\n                $customers-&gt;addAttributeToFilter(\n                    &#091;\n                        &#091;&quot;attribute&quot;=&gt;&quot;firstname&quot;, &quot;like&quot;=&gt;&#039;%&#039;.$query.&#039;%&#039;],\n                        &#091;&quot;attribute&quot;=&gt;&quot;email&quot;,  &quot;like&quot;=&gt;&#039;%&#039;.$query.&#039;%&#039;],\n                        &#091;&quot;attribute&quot;=&gt;&quot;lastname&quot;,  &quot;like&quot;=&gt;&#039;%&#039;.$query.&#039;%&#039;]\n                    ]\n                );\n            }\n            $this-&gt;resultsCount = $customers-&gt;count();\n            if ($customers) {\n                $customers-&gt;setPageSize($pageSize)-&gt;setCurPage($pageNumber);\n            }\n            $customers-&gt;getSelect()\n                -&gt;reset(\\Zend_Db_Select::COLUMNS)\n                -&gt;columns(&#091;&#039;entity_id&#039;, &#039;email&#039;, &#039;firstname&#039;, &#039;lastname&#039;]);\n            $customers-&gt;getSelect()-&gt;reset(\\Zend_Db_Select::ORDER);\n            $customers-&gt;getSelect()-&gt;order(&quot;email&quot;, &quot;DESC&quot;);\n            \n            $data = (!empty($customers)) ? $customers-&gt;getData() : &#091;];\n\n            if (!empty($data)) {\n                foreach ($data as $index =&gt; $value) {\n                    $result          = &#091;];\n                    $fullname = ($value&#091;&quot;firstname&quot;] ?? &quot;&quot;).&quot; &quot;.($value&#091;&quot;lastname&quot;] ?? &quot;&quot;);\n                    $result&#091;&quot;id&quot;]    = $value&#091;&quot;email&quot;] ?? &quot;&quot;;\n                    $result&#091;&quot;text&quot;]  = $fullname.&#039; (&#039;. $result&#091;&quot;id&quot;] .&#039;)&#039;;\n                    $this-&gt;results&#091;] = $result;\n                }\n            }\n        } catch (\\Exception $e) {\n            echo $e-&gt;getMessage();\n            die(&quot;Exception Occurred&quot;);\n        }\n        \n        return $this-&gt;results;\n    }\n}<\/pre>\n\n\n\n<p>7. Now, when we will execute the code and run the controller on the browser. We will get the response as in the following images.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"556\" src=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/09\/Select2-1200x556.png\" alt=\"Select2-js\" class=\"wp-image-305826\" srcset=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/09\/Select2-1200x556.png 1200w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/09\/Select2-300x139.png 300w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/09\/Select2-250x116.png 250w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/09\/Select2-768x356.png 768w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/09\/Select2-1536x711.png 1536w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/09\/Select2.png 1814w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" loading=\"lazy\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1200\" height=\"472\" src=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/09\/Select2Search.png\" alt=\"Select2Search\" class=\"wp-image-305827\" srcset=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/09\/Select2Search.png 1200w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/09\/Select2Search-300x118.png 300w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/09\/Select2Search-250x98.png 250w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/09\/Select2Search-768x302.png 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" loading=\"lazy\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"408\" src=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/09\/Select2GetValue-1200x408.png\" alt=\"Select2GetValue\" class=\"wp-image-305828\" srcset=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/09\/Select2GetValue-1200x408.png 1200w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/09\/Select2GetValue-300x102.png 300w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/09\/Select2GetValue-250x85.png 250w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/09\/Select2GetValue-768x261.png 768w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/09\/Select2GetValue-1536x522.png 1536w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/09\/Select2GetValue.png 1799w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" loading=\"lazy\" \/><\/figure>\n\n\n\n<p>8. <a href=\"https:\/\/github.com\/webkulabhi\/select2-demo\" target=\"_blank\" rel=\"noreferrer noopener\">Get the complete code<\/a>.<\/p>\n\n\n\n<p>Hope this will be helpful. Thanks \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog, we are going to learn how we can create a dropdown or select input type field in a form with dynamic options with the search features. In Magento2, we manage a large number of products and customers, and other data.So, if we are creating a form and we have the need to <a href=\"https:\/\/webkul.com\/blog\/create-dropdown-in-custom-form-using-select2-js-in-magento-2\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":249,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1808,9121],"tags":[12057,12059,12058,12060,12056],"class_list":["post-305825","post","type-post","status-publish","format-standard","hentry","category-ajax","category-magento-2","tag-dropdown-magento2","tag-dynamic-dropdown","tag-dynamic-select-box","tag-select-box-with-ajax-options","tag-select2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Create Dropdown in custom form using select2 js in Magento 2 - Webkul Blog<\/title>\n<meta name=\"description\" content=\"select2 dropdown example, Create Dropdown in custom form using select2 js in Magento 2\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/create-dropdown-in-custom-form-using-select2-js-in-magento-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Create Dropdown in custom form using select2 js in Magento 2 - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"select2 dropdown example, Create Dropdown in custom form using select2 js in Magento 2\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/create-dropdown-in-custom-form-using-select2-js-in-magento-2\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-09-16T19:56:59+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-07-25T07:00:40+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2021\/09\/Select2-1200x556.png\" \/>\n<meta name=\"author\" content=\"Khushboo Sahu\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Khushboo Sahu\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\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:\/\/webkul.com\/blog\/create-dropdown-in-custom-form-using-select2-js-in-magento-2\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/create-dropdown-in-custom-form-using-select2-js-in-magento-2\/\"},\"author\":{\"name\":\"Khushboo Sahu\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/f94b8f53397bf85810761d76c98fadca\"},\"headline\":\"Create Dropdown in custom form using select2 js in Magento 2\",\"datePublished\":\"2021-09-16T19:56:59+00:00\",\"dateModified\":\"2024-07-25T07:00:40+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/create-dropdown-in-custom-form-using-select2-js-in-magento-2\/\"},\"wordCount\":240,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/create-dropdown-in-custom-form-using-select2-js-in-magento-2\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2021\/09\/Select2-1200x556.png\",\"keywords\":[\"dropdown magento2\",\"dynamic dropdown\",\"dynamic select box\",\"select box with ajax options\",\"select2\"],\"articleSection\":[\"Ajax\",\"Magento 2\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/create-dropdown-in-custom-form-using-select2-js-in-magento-2\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/create-dropdown-in-custom-form-using-select2-js-in-magento-2\/\",\"url\":\"https:\/\/webkul.com\/blog\/create-dropdown-in-custom-form-using-select2-js-in-magento-2\/\",\"name\":\"Create Dropdown in custom form using select2 js in Magento 2 - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/create-dropdown-in-custom-form-using-select2-js-in-magento-2\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/create-dropdown-in-custom-form-using-select2-js-in-magento-2\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2021\/09\/Select2-1200x556.png\",\"datePublished\":\"2021-09-16T19:56:59+00:00\",\"dateModified\":\"2024-07-25T07:00:40+00:00\",\"description\":\"select2 dropdown example, Create Dropdown in custom form using select2 js in Magento 2\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/create-dropdown-in-custom-form-using-select2-js-in-magento-2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/create-dropdown-in-custom-form-using-select2-js-in-magento-2\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/create-dropdown-in-custom-form-using-select2-js-in-magento-2\/#primaryimage\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/09\/Select2.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/09\/Select2.png\",\"width\":1814,\"height\":840,\"caption\":\"Select2\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/create-dropdown-in-custom-form-using-select2-js-in-magento-2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Create Dropdown in custom form using select2 js in Magento 2\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/f94b8f53397bf85810761d76c98fadca\",\"name\":\"Khushboo Sahu\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/cabac965df656d114e6bf340df07518c990eda03bb09265dbd5c17f1097adaae?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/cabac965df656d114e6bf340df07518c990eda03bb09265dbd5c17f1097adaae?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g\",\"caption\":\"Khushboo Sahu\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/khushboo-sahu062\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Create Dropdown in custom form using select2 js in Magento 2 - Webkul Blog","description":"select2 dropdown example, Create Dropdown in custom form using select2 js in Magento 2","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:\/\/webkul.com\/blog\/create-dropdown-in-custom-form-using-select2-js-in-magento-2\/","og_locale":"en_US","og_type":"article","og_title":"Create Dropdown in custom form using select2 js in Magento 2 - Webkul Blog","og_description":"select2 dropdown example, Create Dropdown in custom form using select2 js in Magento 2","og_url":"https:\/\/webkul.com\/blog\/create-dropdown-in-custom-form-using-select2-js-in-magento-2\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2021-09-16T19:56:59+00:00","article_modified_time":"2024-07-25T07:00:40+00:00","og_image":[{"url":"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2021\/09\/Select2-1200x556.png","type":"","width":"","height":""}],"author":"Khushboo Sahu","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Khushboo Sahu","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/create-dropdown-in-custom-form-using-select2-js-in-magento-2\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/create-dropdown-in-custom-form-using-select2-js-in-magento-2\/"},"author":{"name":"Khushboo Sahu","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/f94b8f53397bf85810761d76c98fadca"},"headline":"Create Dropdown in custom form using select2 js in Magento 2","datePublished":"2021-09-16T19:56:59+00:00","dateModified":"2024-07-25T07:00:40+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/create-dropdown-in-custom-form-using-select2-js-in-magento-2\/"},"wordCount":240,"commentCount":0,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"image":{"@id":"https:\/\/webkul.com\/blog\/create-dropdown-in-custom-form-using-select2-js-in-magento-2\/#primaryimage"},"thumbnailUrl":"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2021\/09\/Select2-1200x556.png","keywords":["dropdown magento2","dynamic dropdown","dynamic select box","select box with ajax options","select2"],"articleSection":["Ajax","Magento 2"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/create-dropdown-in-custom-form-using-select2-js-in-magento-2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/create-dropdown-in-custom-form-using-select2-js-in-magento-2\/","url":"https:\/\/webkul.com\/blog\/create-dropdown-in-custom-form-using-select2-js-in-magento-2\/","name":"Create Dropdown in custom form using select2 js in Magento 2 - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/webkul.com\/blog\/create-dropdown-in-custom-form-using-select2-js-in-magento-2\/#primaryimage"},"image":{"@id":"https:\/\/webkul.com\/blog\/create-dropdown-in-custom-form-using-select2-js-in-magento-2\/#primaryimage"},"thumbnailUrl":"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2021\/09\/Select2-1200x556.png","datePublished":"2021-09-16T19:56:59+00:00","dateModified":"2024-07-25T07:00:40+00:00","description":"select2 dropdown example, Create Dropdown in custom form using select2 js in Magento 2","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/create-dropdown-in-custom-form-using-select2-js-in-magento-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/create-dropdown-in-custom-form-using-select2-js-in-magento-2\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/create-dropdown-in-custom-form-using-select2-js-in-magento-2\/#primaryimage","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/09\/Select2.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/09\/Select2.png","width":1814,"height":840,"caption":"Select2"},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/create-dropdown-in-custom-form-using-select2-js-in-magento-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Create Dropdown in custom form using select2 js in Magento 2"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/f94b8f53397bf85810761d76c98fadca","name":"Khushboo Sahu","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/cabac965df656d114e6bf340df07518c990eda03bb09265dbd5c17f1097adaae?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cabac965df656d114e6bf340df07518c990eda03bb09265dbd5c17f1097adaae?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g","caption":"Khushboo Sahu"},"url":"https:\/\/webkul.com\/blog\/author\/khushboo-sahu062\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/305825","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/249"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=305825"}],"version-history":[{"count":5,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/305825\/revisions"}],"predecessor-version":[{"id":454610,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/305825\/revisions\/454610"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=305825"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=305825"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=305825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}