In the snippet below, having one checkbox checked at a time works fine but checking multiple checkboxes doesn't work as desired. I'm guessing that when I check multiple checkboxes, for example White and Orange, the function is looking for WhiteOrange in the column cells, obviously leading to no results. How do I check for each label text one by one to get all matching results?
Also, how can I check for the initial state of the checkboxes before the change function so that only the results for White are shown when loading the page? I've tried running the filter function within the for loop before the change function, but it didn't work at all.
Thank you for your help; it is greatly appreciated!
$(function() {
const focusInputs = document.querySelectorAll("input.focusbox");
const focusColumns4 = document.querySelectorAll("table#colors tbody tr td.table-cell:nth-child(4)");
for (const focusInput of focusInputs) {
$(focusInput).on('change', function() {
if (!$(focusInput).is(":checked")) {
$(focusColumns4).parent('tr').show();
} else if ($(focusInput).is(":checked")) {
$(focusColumns4).filter(function() {
return $(this).text() !== $(focusInput).parent().text();
}).parent('tr').hide();
}
});
}
});
body {
font-size: 10px;
}
ul,
li {
list-style-type: none;
margin: 0;
padding: 0;
}
table#colors {
margin-top: 5px;
}
table#colors tbody tr td.entry2 {
width: 200px;
}
.cf-circle {
width: 20px;
height: 20px;
border-radius: 50%;
border: 1px solid #C8C8C8;
-webkit-transition: border-radius 1s, width 1s, height 1s;
transition: border-radius 1s, width 1s, height 1s;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<ul>
<li><label class="focusbox-label"><input class="focusbox" type="checkbox" checked />White</label>
</li>
<li>
<label class="focusbox-label"><input class="focusbox" type="checkbox" />Red</label>
</li>
<li>
<label class="focusbox-label"><input class="focusbox" type="checkbox" />Orange</label>
</li>
</ul>
<table id="colors">
<tbody>
<tr class="head-row">
<th class="table-cell cYellow c1 entry1">Color</th>
<th class="table-cell cYellow c1 entry2">Name</th>
<th class="table-cell cYellow c1 entry3">HTML Color Code</th>
<th class="table-cell cYellow c1 entry4">Shade Group</th>
</tr>
<tr class="s1">
<td class="table-cell entry1">
<div class="color-field cf-default cf-circle" style="background-color: #FFFFFF;"></div>
</td>
<td class="table-cell entry2">White</td>
<td class="table-cell entry3">#FFFFFF</td>
<td class="table-cell entry4">White</td>
</tr>
<tr class="s1">
<td class="table-cell entry1">
<div class="color-field cf-default cf-circle" style="background-color: #F7E7CE;"></div>
</td>
<td class="table-cell entry2">Champagne</td>
<td class="table-cell entry3">#F7E7CE</td>
<td class="table-cell entry4">White</td>
</tr>
<tr class="s1">
<td class="table-cell entry1">
<div class="color-field cf-default cf-circle" style="background-color: #ED2839;"></div>
</td>
<td class="table-cell entry2">Red (Pantone)</td>
<td class="table-cell entry3">#ED2839</td>
<td class="table-cell entry4">Red</td>
</tr>
<tr class="s1">
<td class="table-cell entry1">
<div class="color-field cf-default cf-circle" style="background-color: #ED1B24;"></div>
</td>
<td class="table-cell entry2">Red (CMYK) (pigment red)</td>
<td class="table-cell entry3">#ED1B24</td>
<td class="table-cell entry4">Red</td>
</tr>
<tr class="s1">
<td class="table-cell entry1">
<div class="color-field cf-default cf-circle" style="background-color: #FF8200;"></div>
</td>
<td class="table-cell entry2">UT orange</td>
<td class="table-cell entry3">#FF8200</td>
<td class="table-cell entry4">Orange</td>
</tr>
<tr class="s1">
<td class="table-cell entry1">
<div class="color-field cf-default cf-circle" style="background-color: #FF8000;"></div>
</td>
<td class="table-cell entry2">Orange (wheel)</td>
<td class="table-cell entry3">#FF8000</td>
<td class="table-cell entry4">Orange</td>
</tr>
</tbody>
</table>