Skip to content

Instantly share code, notes, and snippets.

@VictorPietro
Created January 22, 2025 23:01
Show Gist options
  • Save VictorPietro/050316bf2f150b3069228bcd6347e8c8 to your computer and use it in GitHub Desktop.
Save VictorPietro/050316bf2f150b3069228bcd6347e8c8 to your computer and use it in GitHub Desktop.
JetSmartFilters Hide widget before filtering (also optionally hide when no filter is applied anymore). All credits to Crocoblock, just saving this for myself. Add the class to the listing grid that you want to hide.
<style>
body:not(.elementor-editor-active) .hide-listing {
display: none;
height: 0;
}
</style>
<script>
document.addEventListener( 'jet-smart-filters/inited', init );
function hideProvider( $provider, parentSelector ) {
$provider.closest( parentSelector ).addClass( 'hide-listing' );
}
function showProvider( $provider, parentSelector ) {
$provider.closest( parentSelector ).removeClass( 'hide-listing' );
}
function init() {
for ( const groupName in window.JetSmartFilters.filterGroups ) {
const filterGroup = window.JetSmartFilters.filterGroups[ groupName ],
$provider = filterGroup?.$provider,
query = filterGroup?.currentQuery || {};
if ( ! $provider.closest('.hide-before-filter')?.length ) {
continue;
}
if ( ! Object.keys( query ).length ) {
hideProvider( $provider, '.hide-before-filter' );
} else {
showProvider( $provider, '.hide-before-filter' );
}
}
window.JetSmartFilters.events.subscribe( 'ajaxFilters/updated', function( provider, queryId ) {
const filterGroup = window.JetSmartFilters.filterGroups[ provider + '/' + queryId ],
$provider = filterGroup.$provider,
query = filterGroup.currentQuery || {};
if ( ! Object.keys( query ).length ) {
hideProvider( $provider, '.hide-before-filter.hide-on-reset' );
} else {
showProvider( $provider, '.hide-before-filter' );
}
} );
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment