I'm working on filtering a directory that's fed through a component created by our CMS, which I have no control over. The tags for each directory member that I'm trying to filter are inside divs with class of d-none. The script I'm using works except it only displays the items I want filtered when they are the first div inside the card. I'm trying to figure out how to filter a tag if it's not the first item within a card.
For instance, in the following code the filter buttons work until you press the All employees button. Then only Christina's name is displayed because "Employee" is the first tag in her card.
<h1>Employees</h1>
<p>
<button onclick="queryButton('FULL TIME')">Full Time</button>
<button onclick="queryButton('PART TIME')">Part Time</button>
<button onclick="queryButton('SEASONAL')">Seasonal</button>
<button onclick="queryButton('EMPLOYEE')">All Employees</button>
</p>
<div class="cpFeed">
<div class="card-news">
<div style="display: none;" class="d-none">Part Time</div>
<div style="display: none;" class="d-none">Employee</div>
<div>Adele</div>
</div>
<div class="card-news">
<div style="display: none;" class="d-none">Full Time</div>
<div style="display: none;" class="d-none">Employee</div>
<div>Agnes</div>
</div>
<div class="card-news">
<div style="display: none;" class="d-none">Seasonal</div>
<div style="display: none;" class="d-none">Employee</div>
<div>Billy</div>
</div>
<div class="card-news">
<div style="display: none;" class="d-none">Part Time</div>
<div style="display: none;" class="d-none">Employee</div>
<div>Bob</div>
</div>
<div class="card-news">
<div style="display: none;" class="d-none">Seasonal</div>
<div style="display: none;" class="d-none">Employee</div>
<div>Calvin</div>
</div>
<div class="card-news" class="d-none">
<div style="display: none;" class="d-none">Employee</div>
<div style="display: none;" class="d-none">Seasonal</div>
<div>Christina</div>
</div>
<div class="card-news" class="d-none">
<div style="display: none;" class="d-none">Part Time</div>
<div style="display: none;" class="d-none">Employee</div>
<div>Cindy</div>
</div>
</div>
<script>
function queryButton(queryFilter) {
var filter, feedDiv, cardDiv, unnamedDiv, i, txtValue;
filter = queryFilter;
feedDiv = document.querySelector("div.cpFeed");
cardDiv = feedDiv.querySelectorAll("div.card-news");
for (i = 0; i < cardDiv.length; i++) {
unnamedDiv = cardDiv[i].querySelector(".d-none");
[0];
txtValue = unnamedDiv.textContent || div.div.innerHTML;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
cardDiv[i].style.display = "";
} else {
cardDiv[i].style.display = "none";
}
}
}
</script>
I tried changing the querySelector to querySelectorAll in the following line. The
unnamedDiv = cardDiv[i].querySelectorAll(".d-none");
This causes the buttons' functions to stop working. I get the following error in the console:
Uncaught ReferenceError: div is not defined
at queryButton (example.html:56:44)
at HTMLButtonElement.onclick (example.html:5:45)
Is there something I'm missing?
.carnewsdivs?