I need to get elements from a table that has header for each row. It is something like this:
<table>
<td>
<table>
<tr>
<th>Name</th>
<th>Test name</th>
<th>Percentage</th>
</tr>
<tr>....contents(multiple rows of marks for a candidate)........ </tr>
</table>
</td>
<td>
<table>
<tr>
<th>Name</th>
<th>Test name</th>
<th>Percentage</th>
</tr>
<tr>....contents(multiple rows of marks for a candidate)........ </tr>
</table>
</td>
<td>
<table>
<tr>
<th>Name</th>
<th>Test name</th>
<th>Percentage</th>
</tr>
<tr>....contents(multiple rows of marks for a candidate)........ </tr>
</table>
</td>
</table>
So basically each row of the main table is a table consisting of multiple rows with its own header. I need to get elements of each row table on click on header. I must do this using vanilla Javascript. I should not use any jquery related plugin. That is the requirement. So how can I access each row on clicking the header for that row? At present the className for each row is the same. So when I use getElementsByClassName, I get multiple times of each row. For eg: if there are five rows then I get 5 times of each rows equaling 25 times on each click. If I use setAttribute function then I get the rows only the first time I click the button because I change the className. So how should I go about solving this problem?
getTable.js
th = document.getElementsByTagName('th');
for(let c=0; c < th.length; c++){
th[c].addEventListener('click',item(c));
}
function item(c){
return function(){
console.log(c);
sortTable(c);
}
}
function sortTable(c) {
let table;
table = Array.prototype.slice.call(document.getElementsByClassName("the_assessments"));
for(let j=0; j<table.length; j++) {
table[j].setAttribute("class","assessment_table");
console.log(table[j]);
}
<tr>and a<td>.