I have implemented a table in my HTML with an input checkbox present in each row.
Currently, I am attempting to determine whether the checkbox has been selected or not. Below is the approach I have taken:
HTML:
<table class="table table-hover" id="just_a_table">
<thead>
<tr>
<th scope="col">Select</th>
<th scope="col">IP Addr</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let data of instances">
<td><input type="checkbox" (click)='onSelect()'></td>
<td>{{data["IP"]}}</td>
</tr>
</tbody>
</table>
TS:
onSelect() {
// Get table data from the html and check if the checkbox is active.
// Access rows of the table using "rows" object and cells using "cells" object.
this.table_data = document.getElementById("just_a_table")
for (var i=1, row; row = this.table_data.rows[i]){
for (var j=0, cell; cell = row.cells[j]){
if (<HTMLInputElement>cell[1].checked){
console.log("It is checked")
}
}
}
}
I decided to take this method as I prefer not to target the input element by its ID to check its status.
If you have any suggestions or guidance on how I can achieve this more efficiently, it would be greatly appreciated.