{"id":8030,"date":"2015-11-21T13:45:40","date_gmt":"2015-11-21T13:45:40","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/11\/21\/resetting-select2-value-in-dropdown-with-reset-button-open-source-projects-select2-select2\/"},"modified":"2022-08-30T15:03:03","modified_gmt":"2022-08-30T15:03:03","slug":"resetting-select2-value-in-dropdown-with-reset-button-open-source-projects-select2-select2","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/11\/21\/resetting-select2-value-in-dropdown-with-reset-button-open-source-projects-select2-select2\/","title":{"rendered":"Resetting Select2 value in dropdown with reset button-open source projects select2\/select2"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/0fbaa696710100a0f934686b90fdb5e9?s=128&amp;d=identicon&amp;r=PG\" \/> <strong>Ifedi Okonkwo<\/strong><\/p>\n<p>To achieve a generic solution, why not do this:<\/p>\n<pre><code>$(':reset').live('click', function(){\n    var $r = $(this);\n    setTimeout(function(){ \n        $r.closest('form').find('.select2-offscreen').trigger('change'); \n    }, 10);\n});\n<\/code><\/pre>\n<p>This way: You&#8217;ll not have to make a new logic for each select2 on your application.<\/p>\n<p>And, you don&#8217;t have to know the default value (which, by the way, does not have to be <code>\"\"<\/code> or even the first option)<\/p>\n<p>Finally, setting the value to <code>:selected<\/code> would not always achieve a true reset, since the current <em>selected<\/em> might well have been set programmatically on the client, whereas the default action of the <code>form select<\/code> is to return input element values to the server-sent ones.<\/p>\n<p><strong>EDIT:<\/strong> Alternatively, considering the deprecated status of <code>live<\/code>, we could replace the first line with this:<\/p>\n<pre><code>$('form:has(:reset)').on('click', ':reset', function(){\n<\/code><\/pre>\n<p>or better still:<\/p>\n<pre><code>$('form:has(:reset)').on('reset', function(){\n<\/code><\/pre>\n<p><strong>PS:<\/strong> I personally feel that resetting on reset, as well as triggering <code>blur<\/code> and <code>focus<\/code> events attached to the original select, are some of the most conspicuous &#8220;missing&#8221; features in select2!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ifedi Okonkwo To achieve a generic solution, why not do this: $(&#8216;:reset&#8217;).live(&#8216;click&#8217;, function(){ var $r = $(this); setTimeout(function(){ $r.closest(&#8216;form&#8217;).find(&#8216;.select2-offscreen&#8217;).trigger(&#8216;change&#8217;); }, 10); }); This way: You&#8217;ll not have to make a new logic for each select2 on your application. And, you don&#8217;t have to know the default value (which, by the way, does not have to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8030","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8030","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=8030"}],"version-history":[{"count":1,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8030\/revisions"}],"predecessor-version":[{"id":8681,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8030\/revisions\/8681"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=8030"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=8030"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=8030"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}