I'm trying to create table where I can filter my data using a checkbox per column
by create function a and function b for check filter column
Problem : I want to check column to filter table contents on clean code and easy to use (btw I don't understand how to use arrow functions)
have any way edit for my clean code ?
const a = function filter(event) {
var element = event.target
var condt1 = document.getElementsByClassName("check1")
for (var i = 0; i < condt1.length; i++) {
if (condt1[i].innerHTML.toLowerCase() == element.value.toLowerCase()) {
if (element.checked == true) {
condt1[i].parentElement.style = ""
} else {
condt1[i].parentElement.style = "display:none"
}
}
}
}
const b = function filter(event) {
var element = event.target
var condt1 = document.getElementsByClassName("check2")
for (var j = 0; j < condt1.length; j++) {
if (condt1[j].innerHTML.toLowerCase() == element.value.toLowerCase()) {
if (element.checked == true) {
condt1[j].parentElement.style = ""
} else {
condt1[j].parentElement.style = "display:none"
}
}
}
}
document.querySelectorAll('.option1')
.forEach(input => input.addEventListener('input', a));
document.querySelectorAll('.option2')
.forEach(input => input.addEventListener('input', b));
table {
border-collapse : collapse;
margin : 2em 1em;
}
td,th {
padding : .2em .8em;
border : 1px solid darkblue;
}
<div id="input">
<label>Filter Name </label><br>
<label>Human<input class="option1" type="checkbox" value="Human" checked/></label>
<label>Robot<input class="option1" type="checkbox" value="Robot"checked/></label><br><br>
<label>Filter boolean </label><br>
<label>true<input class="option2" type="checkbox" value="true" checked/></label>
<label>false<input class="option2" type="checkbox" value="false" checked/></label>
</div>
<table id="my-table">
<thead>
<tr>
<th> Name </th>
<th> boolean </th>
</tr>
</thead>
<tbody>
<tr>
<td class="check1">Human</td>
<td class="check2">true</td>
</tr>
<tr>
<td class="check1">Robot</td>
<td class="check2">false</td>
</tr>
<tr>
<td class="check1">Human</td>
<td class="check2">true</td>
</tr>
<tr>
<td class="check1">false</td>
<td class="check2">true</td>
</tr>
</tbody>
</table>
Sorry for my bad English, can't explain all what I need, hope you understand what I need
Thanks !
Nameoffalselooks like a typo