Skip Navigation

[Closed] Javascript for conditional filtering

This support ticket is created 4 years, 11 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 13:00 – 18:00 13:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

Tagged: 

This topic contains 3 replies, has 2 voices.

Last updated by Raja Mohammed 4 years, 11 months ago.

Assisted by: Raja Mohammed.

Author
Posts
#1854093

Tell us what you are trying to do?

Hide search filters based on other search filter selections. These filters are based on taxonomies.

Code as below

<select name="wpv-genre" class="js-wpv-filter-trigger form-control">
<option selected="selected" value="0">All</option>
<option value="thriller">thriller</option>
<option value="comedy">comedy</option>
</select>

<div class="form-group"><label class="wpv-custom-search-filter__label" for="format">Formats</label>
<!-- ## --><span class="wpv-custom-search-filter__input">
<select name="wpv-format" class="js-wpv-filter-trigger form-control">
<option selected="selected" value="0">All</option>
<option value="written">Written</option>
</select></span></div>

The below code is the Javascript that should work but it hides all search boxes as they are all styled with the div class="form-group".

Is there a way I can amend the below so that the var element only focuses on the var element with the label class for "format" rather than all div class which = "form-group"

var element = document.querySelector("div.form-group");

document.querySelector('select[name=wpv-genre]').addEventListener('change', function(){
if(this.value == 'thriller')
element.style.display = 'none';
else
element.style.display = 'block';
})

#1854453

Raja Mohammed
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello there,

I would recommend consider using jQuery parent function to target the form-group div wrapper based on the selector.

(function($) {
$(document).on('change','select[name="wpv-genre"]',function(){
 if($(this).val() == 'thriller') {
 $('select[name="wpv-format"]).parent('div.form-group').hide();
 }
else{
  $('select[name="wpv-format"]).parent('div.form-group').show();
}

}
})(jQuery);

Reference : hidden link

Regards
Raja

#1857109

Hi there,

Unfortunately, the jQuery had no effect.

The full html for the site search filters is below:

<div class="wp-block-group"><div class="wp-block-group__inner-container"></div></div>

<div class="wp-block-toolset-views-custom-search-filter wpv-custom-search-filter wpv-custom-search-filter-label-top alignleft" data-toolset-views-custom-search-filter="1168e349606b2f669de6232a2eec50c9"><div><div class="form-group"><label class="wpv-custom-search-filter__label" for="class">Genre</label><!-- ## --><span class="wpv-custom-search-filter__input"><select name="wpv-genre" class="js-wpv-filter-trigger form-control"><option selected='selected' value="0">All</option><option value="thriller">Thriller</option><option value="comedy">  -Comedy</option></select></span></div></div></div>

<div class="wp-block-toolset-views-custom-search-filter wpv-custom-search-filter wpv-custom-search-filter-label-top alignleft" data-toolset-views-custom-search-filter="dbc38c6ca99216b2af43df4cd2c711e7"><div><div class="form-group"><label class="wpv-custom-search-filter__label" for="format">Format</label><!-- ## --><span class="wpv-custom-search-filter__input"><select name="wpv-format" class="js-wpv-filter-trigger form-control"><option selected='selected' value="0">All</option><option value="written">Written</option></select></span></div></div></div>

Please let me know why the jQuery won't work for the above html or if there is an alternative. Thanks

#1857667

Raja Mohammed
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Sorry there was a minor syntax error in the JS snippet a missing Quote. can you please try the JS snippet below . If the still doesn't work please share the URL where the form can be seen so that i can try the working the code.


(function($) {
$(document).on('change','select[name="wpv-genre"]',function(){
 if($(this).val() == 'thriller') {
 $('select[name="wpv-format"]').parent('div.form-group').hide();
 }
else{
  $('select[name="wpv-format"]').parent('div.form-group').show();
}
 
}
})(jQuery);

The topic ‘[Closed] Javascript for conditional filtering’ is closed to new replies.