{"id":7314803,"date":"2016-04-25T02:23:26","date_gmt":"2016-04-25T02:23:26","guid":{"rendered":"https:\/\/wordpress.org\/support\/topic\/issue-with-radio-buttons-1\/"},"modified":"2016-08-31T16:52:16","modified_gmt":"2016-08-31T16:52:16","slug":"issue-with-radio-buttons-1","status":"publish","type":"topic","link":"https:\/\/wordpress.org\/support\/topic\/issue-with-radio-buttons-1\/","title":{"rendered":"issue with radio buttons"},"content":{"rendered":"<p>Appreciate that this is an edge case (as the default search form doesn&#8217;t have any radio buttons) but the js that gathers the form content to make the live query doesn&#8217;t handle radios correctly; it includes the value from the last radio button, whether checked or not. <\/p>\n<p>Discovered this when I was creating a modified form that had a taxonomy filter on it.<\/p>\n<p>Changing the parameter gathering in <code>daves-wordpress-live-search.js<\/code> (line 225) to the following, fixes the issue:<\/p>\n<pre><code>\/\/ Marshall the parameters into an object\n\t\tvar parameters = {};\n                var fields = searchBox.parents(&#039;form&#039;).find(&#039;input:not(:submit),select,textarea,input:not(:radio)&#039;);\n                for(fieldIndex in fields) {\n                    if(fields.hasOwnProperty(fieldIndex) &amp;&amp; \/* test for integer *\/ fieldIndex % 1 === 0) {\n                        var field = jQuery(fields[fieldIndex]);\n                        parameters[field.attr(&#039;name&#039;)] = field.val();\n                    }\n                }\n                \/\/ handle radio groups cleanly\n                var radioNames = [];\n                searchBox.parents(&#039;form&#039;).find(&#039;input:radio&#039;).each(function(radioIndex) {\n                    var rName = jQuery(this).attr(&#039;name&#039;);\n                    if (radioNames.indexOf(rName) == -1) {radioNames.push(rName)}\n                });\n                for(var radioNameIndex in radioNames) {\n                    var radioName = radioNames[radioNameIndex];\n                    var selected = searchBox.parents(&#039;form&#039;).find(&quot;input[type=&#039;radio&#039;][name=&#039;&quot; + radioName + &quot;&#039;]:checked&quot;);\n                    if (selected.length &gt; 0) {\n                        parameters[radioName]  = selected.val();\n                    }\n                }<\/code><\/pre>\n<p>I can&#8217;t see if there&#8217;s a cleaner way to do this (for instance, populate a hidden field with the radio value each time one is clicked) but that appeared to add further complexity.<\/p>\n<p>https:\/\/wordpress.org\/plugins\/daves-wordpress-live-search\/<\/p>\n","protected":false},"template":"","class_list":["post-7314803","topic","type-topic","status-publish","hentry","topic-tag-javascript","topic-tag-radio-buttons"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/7314803","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/7314803\/revisions"}],"wp:attachment":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=7314803"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}