Forums › Forums › Search & Filter Pro › Reset filters with javascript
Tagged: javascript, reset, trigger
- This topic has 26 replies, 3 voices, and was last updated 4 years, 7 months ago by Ross.
-
Trevor(Private) April 6, 2020 at 11:19 am #239128
I am not sure there is a simple answer until V3 releases (a few weeks away we hope), but this might give you some ideas:
https://support.searchandfilter.com/forums/topic/reset-filters-on-search-results-page/#post-183441
Anonymous(Private) April 6, 2020 at 1:48 pm #239138Hello Trevor,
I tried to implement this solution but it doesn’t work, maybe because I’m using ajax ?
The problem is that my radio buttons disappear when I use another filter and it breaks my script.jQuery(document).ready(function ($) { const depts = document.querySelectorAll('#departements path'); const results = document.querySelector('.search-filter-results'); const villes = document.querySelector('.sf-field-taxonomy-ville'); const services = document.querySelector('.sf-field-taxonomy-service'); const reset = document.querySelector('.sf-field-reset'); function handleDeptClick(e) { depts.forEach(dept => { dept.classList.remove('active'); }); const deptPath = e.target; const departementNum = deptPath.className.baseVal.split('-').pop(); console.log(<code>departementNum: ${departementNum}</code>); deptPath.classList.add('active'); const inputRadioSelector = <code>input.sf-input-radio[value="${departementNum}"]</code>; const inputRadio = document.querySelector(inputRadioSelector); if ( inputRadio.checked === false ) { // FAILS BECAUSE inputRadio IS EQUAL TO NULL IF WE USE ANOTHER SET OF FILTERS inputRadio.checked = true; } while(results.firstChild) results.removeChild(results.firstChild); jQuery('.searchandfilter').submit(); } depts.forEach(dept => { dept.addEventListener('click', handleDeptClick); }); });
Ross Moderator(Private) April 6, 2020 at 2:46 pm #239146Hi David
The link Trevor provided uses our
sf:init
event, to capture the search form object into the variablesearchForm
which you can then use to perform a.reset()
on it.Are you saying you did this, and it didn’t work?
Can you show me the code you tried, using the example code Trevor sent?
Thanks
Anonymous(Private) April 6, 2020 at 3:22 pm #239150Hi Ross,
thanks for your help !
this is my code :
jQuery(document).ready(function ($) { var searchForm = {}; function resetForm(){ searchForm.resetForm(false); } $(document).on("sf:init", ".searchandfilter", function(e, data){ searchForm = data.object; }); const depts = document.querySelectorAll('#departements path'); const results = document.querySelector('.search-filter-results'); const villes = document.querySelector('.sf-field-taxonomy-ville'); const services = document.querySelector('.sf-field-taxonomy-service'); const reset = document.querySelector('.sf-field-reset'); function handleDeptClick(e) { resetForm(); depts.forEach(dept => { dept.classList.remove('active'); }); const deptPath = e.target; const departementNum = deptPath.className.baseVal.split('-').pop(); deptPath.classList.add('active'); const inputRadioSelector = <code>input.sf-input-radio[value="${departementNum}"]</code>; const inputRadio = document.querySelector(inputRadioSelector); if ( inputRadio.checked === false ) { inputRadio.checked = true; } results.classList.add('active'); while(results.firstChild) results.removeChild(results.firstChild); jQuery('.searchandfilter').submit(); } depts.forEach(dept => { dept.addEventListener('click', handleDeptClick); }); });
searchForm is equal to an empty object, maybe I miss something here
and the form doesn’t reset (but I have no error)Thanks,
David
Ross Moderator(Private) April 6, 2020 at 3:54 pm #239154Hi David
I think
sf:init
may be firing before you hook into it – did you try aconsole.log
to see if event was triggered? – try bringing that code block outside ofjQuery(document).ready
Check the last example here:
https://searchandfilter.com/documentation/faq/#will-my-lightbox-or-other-fancy-javascript-work-with-this-pluginOf the ideal JS structure to hook into the event – its after the comment
//depending on where you add your JS, sometimes its necessar....
Let me know how you get on.
Thanks
Anonymous(Private) April 6, 2020 at 4:34 pm #239161Hi Ross,
I tried this (and many other things) :
(function ( $ ) { "use strict"; var searchForm = {}; function resetForm(){ searchForm.resetForm(false); } $(document).on("sf:init", ".searchandfilter", function(e, data){ searchForm = data.object; console.log("S&F JS initialised"); }); $(document).ready(function ($) { const depts = document.querySelectorAll('#departements path'); const results = document.querySelector('.search-filter-results'); const villes = document.querySelector('.sf-field-taxonomy-ville'); const services = document.querySelector('.sf-field-taxonomy-service'); const reset = document.querySelector('.sf-field-reset'); function handleDeptClick(e) { resetForm(); depts.forEach(dept => { dept.classList.remove('active'); }); const deptPath = e.target; const inputRadioNull = document.querySelector('.sf-item-0 input.sf-input-radio'); inputRadioNull.checked = true; const departementNum = deptPath.className.baseVal.split('-').pop(); deptPath.classList.add('active'); const inputRadioSelector = <code>input.sf-input-radio[value="${departementNum}"]</code>; const inputRadio = document.querySelector(inputRadioSelector); if ( inputRadio.checked === false ) { inputRadio.checked = true; } while(results.firstChild) results.removeChild(results.firstChild); $('.searchandfilter').submit(); } depts.forEach(dept => { dept.addEventListener('click', handleDeptClick); }); }); }(jQuery));
The
sf:init
event seems to be triggered. My console says : “S&F JS initialised”I always have an error on this line :
if ( inputRadio.checked === false ) {
because inputRadio is null (cause it’s not in the DOM anymore, the resetForm function doesn’t seem to work).
Uncaught TypeError: Cannot read property 'checked' of null
David
-
AuthorPosts