Skip Navigation

[Resolved] Hide search filter options based on value of checkbox

This support ticket is created 3 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 7 replies, has 2 voices.

Last updated by Purchasing WCER 3 years, 3 months ago.

Assisted by: Shane.

Author
Posts
#2143955

Tell us what you are trying to do? Want to hide all ELA, Math, and Science curriculum field options class="subject-options" until a wpv-wpcf-type checkbox is selected then show the additional filters based on the subject selection. Could show 1-3 options depending on # of checkboxes checked. What I have below works for a second but as soon as the results load it hides the subject-options again.

<div class="form-group">
<label for="wpv-wpcf-type">[wpml-string context="wpv-views"]Subject[/wpml-string]</label>
[wpv-control-postmeta type="checkboxes" field="wpcf-type" url_param="wpv-wpcf-type"]
</div>

<div class="form-group ELA subject-options">
<label for="wpv-wpcf-ela-curriculum">[wpml-string context="wpv-views"]ELA Curriculum[/wpml-string]</label>
[wpv-control-postmeta type="multi-select" field="wpcf-ela-curriculum" url_param="wpv-wpcf-ela-curriculum"]
</div>
<div class="form-group Math subject-options">
<label for="wpv-wpcf-math-curriculum">[wpml-string context="wpv-views"]Math Curriculum[/wpml-string]</label>
[wpv-control-postmeta type="multi-select" field="wpcf-math-curriculum" url_param="wpv-wpcf-math-curriculum"]
</div>
<div class="form-group Science subject-options">
<label for="wpv-wpcf-science-curriculum">[wpml-string context="wpv-views"]Science Curriculum[/wpml-string]</label>
[wpv-control-postmeta type="multi-select" field="wpcf-science-curriculum" url_param="wpv-wpcf-science-curriculum"]
</div>

I have this code in the JS editor for the search and pagination section:
( function( $ ) {
$(document).ready(function(){
$('input[type="checkbox"]').click(function(){
var val = $(this).attr("value");
$("." + val).toggle();
});
});
})( jQuery );

CSS editor:
.subject-options{
display:none;
}

Under Pagination and Slider settings: I've tried adjusting how the results are updated (AJAX, manual, etc.) and the adjusting URLs after load or not and neither of those made a difference.

URL to view search: hidden link

#2144049

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Dung,

Thank you for getting in touch. I believe the issue here is that you need to use the callback function for the JS to ensure that function triggers to fields to remain open.
var atLeastOneIsChecked = $('#checkArray:checkbox:checked').length > 0;

jQuery( document ).on( 'js_event_wpv_parametric_search_results_updated', function( event, data ) {
	/**
	* data.view_unique_id (string) The View unique ID hash
	* data.layout (object) The jQuery object for the View layout wrapper
	*/
( function( $ ) {
$(document).ready(function(){
$('input[type="checkbox"]').click(function(){
var val = $(this).attr("value");
$("." + val).toggle();
});
});
})( jQuery );

CSS editor:
.subject-options{
display:none;
}
	
});

However you will need to modify the function to check if the field has a value selected rather than if it is clicked, so when the page refreshes the function checks and enables the other fields.

Please let me know if this helps.
Thanks,
Shane

#2144101

Hi Shane,

I'm not able to get that working. I'm also not sure what your comments mean. The code below works if the checkbox is already checked then the page is refreshed. But doesn't work as items are checked. I'm a novice with JQuery. Can you please advise?

( function( $ ) {
$(document).ready(function(){
if ($("#form-5c06ee531b8094422f999a7d12b10937-1:checked").length > 0) {
$(".ELA").show();
}
if ($("#form-5c06ee531b8094422f999a7d12b10937-1:checked").length < 0) {
$(".ELA").hide();
}
});
})( jQuery );

#2144759

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Dung,

Would you mind allowing me to have admin access to the site so that I can see if I can provide a solution for this ?

I have an idea of using your browser's localstorage to store the selected value and retrieve the value after the page refreshes so that we can know which fields were selected.

I've enabled the private fields for your next response.
Thanks,
Shane

#2144873

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Dung,

Have a look now, i've added the solution to your page.

Let me know if everything is ok now.

Thanks,
Shane

#2144889

Hi Shane,

Thank you for taking a look. It's close. When Subject: English Language Arts is checked, the correct option shows. When Math is also checked the English options hide and Math are shown. Both should show. If all three are checked in order--then Science is checked--Science still shows. In this case, only English and Math should show. How do we get more than one option to show at a time and for the options to hide when subjects are unchecked?

#2145063

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Dung,

I took another look at this for you and was able to resolve the issue.

Can you check the site now and let me know if everything is working correctly.

Thanks,
Shane

#2145067

Amazing. Thank you so much, Shane!