{"id":1859,"date":"2018-04-25T20:41:30","date_gmt":"2018-04-26T01:41:30","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=1859"},"modified":"2018-08-23T09:01:12","modified_gmt":"2018-08-23T14:01:12","slug":"jquery-use-jquery-reset-select-menus-text-fields","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2018\/04\/25\/jquery-use-jquery-reset-select-menus-text-fields\/","title":{"rendered":"jQuery: How to use jQuery to reset select menus and text fields"},"content":{"rendered":"<p>jQuery is a powerful tool in that it allows us to select any DOM element and perform some action on it. In this example, I will show how to use jQuery to select IDs from select menus and a text field and reset them when clicking a div.<\/p>\n<p>&nbsp;<\/p>\n<h4 class=\"light-blue\"><strong>Example 1 &#8211; Resetting selected<\/strong><\/h4>\n<p>This example shows how to reset a select menu to the default &#8216;selected&#8217; option. Note that the selected option can be anywhere in the list of options, it doesn&#8217;t have to be the first option in the list. I won&#8217;t show the CSS, but it will be visible in the embedded codepen.<\/p>\n<p><strong>HTML<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">&lt;div class=\"container\"&gt;\r\n  &lt;h2 class=\"light-blue\"&gt;Example 1 - Resetting selected&lt;\/h2&gt;\r\n  &lt;select id=\"fruitSelect\"&gt;\r\n      &lt;option value=\"a\" selected&gt;apple&lt;\/option&gt;\r\n      &lt;option value=\"b\"&gt;banana&lt;\/option&gt;\r\n      &lt;option value=\"c\"&gt;cherry&lt;\/option&gt;\r\n      &lt;option value=\"d\"&gt;date&lt;\/option&gt;\r\n  &lt;\/select&gt;\r\n  &lt;div id=\"reset1\" class=\"reset\" style=\"cursor: pointer;\"&gt;Reset Menu&lt;\/div&gt;\r\n&lt;\/div&gt;\r\n<\/pre>\n<p><strong>jQuery<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">$(\"#reset1\").on(\"click\", function () {\r\n    $('#fruitSelect option').prop('selected', function() {\r\n        return this.defaultSelected;\r\n    });\r\n});<\/pre>\n<p>&nbsp;<\/p>\n<h4 class=\"light-blue\"><strong>Example 2 &#8211; Resetting to the first option<\/strong><\/h4>\n<p>Notice the jQuery changes <code class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">selectedIndex<\/code> to index 0:<\/p>\n<p><strong>HTML<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">&lt;div class=\"container\"&gt;\r\n  &lt;h2 class=\"green\"&gt;Example 2 - Resetting to the first option&lt;\/h2&gt;\r\n  &lt;select id=\"stateSelect\"&gt;\r\n      &lt;option &gt;Select a state you lived in&lt;\/option&gt;\r\n      &lt;option &gt;California&lt;\/option&gt;\r\n      &lt;option &gt;Oregon&lt;\/option&gt;\r\n      &lt;option &gt;Florida&lt;\/option&gt;\r\n      &lt;option &gt;North Dakota&lt;\/option&gt;\r\n  &lt;\/select&gt;\r\n  &lt;div id=\"reset2\" class=\"reset\" style=\"cursor: pointer;\"&gt;Reset Menu&lt;\/div&gt;\r\n&lt;\/div&gt;<\/pre>\n<p><strong>jQuery<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">$(\"#reset2\").on(\"click\", function () {\r\n $('#stateSelect').prop('selectedIndex', 0);\r\n});<\/pre>\n<p>&nbsp;<\/p>\n<h4 class=\"light-blue\"><strong>Example 3 &#8211; Resetting a text field<\/strong><\/h4>\n<p>Notice the jQuery replaces the text using <code class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">.val(\"\")<\/code>.<\/p>\n<p><strong>HTML<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">&lt;div class=\"container\"&gt;\r\n   &lt;h2 class=\"orange\"&gt;Example 3 - Resetting a text field&lt;\/h2&gt;\r\n   &lt;input type=\"text\" size=30 value=\"I typed some text here!\" id=\"textInput\"&gt;\r\n   &lt;br&gt;&lt;br&gt;\r\n   &lt;div id=\"reset3\" class=\"reset\" style=\"cursor: pointer;\"&gt;Clear Text Field&lt;\/div&gt;\r\n&lt;\/div&gt;<\/pre>\n<p><strong>jQuery<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">$(\"#reset3\").on(\"click\", function () {\r\n $('#textInput').val(\"\");\r\n});<\/pre>\n<p>&nbsp;<\/p>\n<h4 class=\"light-blue\"><strong>Example 4 &#8211; Resetting all fields with one click<br \/>\n<\/strong><\/h4>\n<h4 class=\"light-blue\"><strong>\u00a0<\/strong><\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">$(\"#reset4\").on(\"click\", function () {\r\n    let selectMenus = ['fruitSelect', 'stateSelect'];\r\n    let textFields = ['textInput'];\r\n    for(i = 0; i &lt; selectMenus.length; i++) {\r\n      $('#' + selectMenus[i]).prop('selectedIndex', 0);\r\n    }\r\n    for(j = 0; j &lt; textFields.length; j++) {\r\n      $('#' + textFields[j]).val(\"\");\r\n    }\r\n});<\/pre>\n<p>Here are the examples, with full CSS in codepen:<strong>\u00a0<\/strong><\/p>\n<p class='codepen'  data-height='900' data-theme-id='0' data-slug-hash='PezrRB' data-default-tab='html,result' data-animations='run' data-editable='' data-embed-version='2'>\nSee the Pen How to use jQuery to reset select menus and text fields by Chris Nielsen (@Chris_Nielsen) on CodePen.<\/p>\n\n<p>&nbsp;<\/p>\n<p>For more information about these jQuery features, see:<br \/>\n<a href=\"https:\/\/www.w3schools.com\/jsref\/prop_select_selectedindex.asp\">https:\/\/www.w3schools.com\/jsref\/prop_select_selectedindex.asp<\/a><br \/>\n<a href=\"http:\/\/api.jquery.com\/?s=selectedIndex\">http:\/\/api.jquery.com\/?s=selectedIndex<\/a><br \/>\n<a href=\"http:\/\/api.jquery.com\/prop\/\">http:\/\/api.jquery.com\/prop\/<\/a><br \/>\n<a href=\"https:\/\/www.w3schools.com\/jquery\/html_prop.asp\">https:\/\/www.w3schools.com\/jquery\/html_prop.asp<\/a><br \/>\n<a href=\"http:\/\/api.jquery.com\/val\/\">http:\/\/api.jquery.com\/val\/<\/a><br \/>\n<a href=\"https:\/\/www.w3schools.com\/jquery\/html_val.asp\">https:\/\/www.w3schools.com\/jquery\/html_val.asp<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>jQuery is a powerful tool in that it allows us to select any DOM element and perform some action on it. In this example, I will show how to use jQuery to select IDs from select menus and a text field and reset them when clicking a div. &nbsp; Example 1 &#8211; Resetting selected This &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2018\/04\/25\/jquery-use-jquery-reset-select-menus-text-fields\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">jQuery: How to use jQuery to reset select menus and text fields<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44,128],"tags":[10,105,45,123,176,177],"class_list":["post-1859","post","type-post","status-publish","format-standard","hentry","category-javascript-language","category-jquery","tag-css","tag-html","tag-javascript","tag-jquery","tag-prop","tag-val"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1859","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=1859"}],"version-history":[{"count":10,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1859\/revisions"}],"predecessor-version":[{"id":1869,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1859\/revisions\/1869"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=1859"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=1859"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=1859"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}