I need help with a jQuery script that can hide certain elements based on the selected option value from a drop-down menu. I want to be able to read the text content of the selected option value and then hide specific div classes accordingly:
<div class="filter_options">
<input id="search_query" type="hidden" value="Texts" />
<fieldset class="filterType">
<label>Filter By Type</label><select id="search_filter_type">
<option value="">All</option>
<option value="Cat">Cat</option>
<option value="Dog">Dog</option>
</select>
</fieldset>
</div>
The goal is to hide the div classes with the "pg_list" class that do not contain the selected value from the drop-down menu.
<div class="pg_list">
<p>This text contains a Dog</p>
</div>
<div class="pg_list">
<p>This text contains a Cat</p>
</div>
<div class="pg_list">
<p>This text contains a Rabbit</p>
</div>
For example, if the user selects "Cat" from the drop-down menu, it should hide the div containing "Dog" or "Rabbit", leaving only the one containing "Cat".
I have already prepared the necessary CSS:
.displayNone{
display: none;
}
Here is my existing jQuery code, which currently works for an input text field but needs modification to work with a selected drop-down value:
$('input').on('keypress keyup', function(){
var value = $(this).val().toLowerCase();
if (value != '') {
$('.pg_list').each(function () {
if ($(this).text().toLowerCase().indexOf(value) > -1) {
$(this).removeClass('displayNone');
} else {
$(this).addClass('displayNone');
}
});
} else {
$('.pg_list').removeClass('displayNone');
}
Thank you for your assistance!