I am trying to sort several tables using the same function within Javascript. The tables will all be similar, with me just wanting it to function that when you click on the table heading, it will sort alphabetically or in numerical order (highest to lowest), and when you click on the header again, it will sort that row in the reverse direction (e.g. lowest to highest).
I have used this code from W3 Schools: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_sort_table_desc
It works just fine for sorting just one table. However it doesn't work when I change 'GetElementById' to 'GetElementsByClassName'.
Below is my altered HTML code: Ignore the NFL content, as it's just data I'm using to test out the table contents)
<table border="1" class="supTable">
<tr>
<th onclick="sortTable(0)"> Team Name</th>
<th onclick="sortTable(1)">Super Bowls</th>
</tr>
<tr>
<td>Atlanta Falcons</td>
<td>0</td>
</tr>
<tr>
<td>Dallas Cowboys</td>
<td>4</td>
</tr>
<tr>
<td>Houston Texans</td>
<td>0</td>
</tr>
<tr>
<td>Green Bay Packers</td>
<td>3</td>
</tr>
<tr>
<td>New England Patriots</td>
<td>5</td>
</tr>
<tr>
<td>Oakland Raiders</td>
<td>2</td>
</tr>
<tr>
<td>New York Giants</td>
<td>2</td>
</tr>
<tr>
<td>Miami Dolphins</td>
<td>1</td>
</tr>
</table>
</div>
<div id="supAfc">
<table border="1" class="supTable">
<tr>
<th onclick="sortTable(0)"> Team Name</th>
<th onclick="sortTable(1)">Super Bowls</th>
</tr>
<tr>
<td>Houston Texans</td>
<td>0</td>
</tr>
<tr>
<td>New England Patriots</td>
<td>5</td>
</tr>
<tr>
<td>Oakland Raiders</td>
<td>2</td>
</tr>
<tr>
<td>Miami Dolphins</td>
<td>1</td>
</tr>
</table>
</div>
<div id="supNfc">
<table border="1" class="supTable">
<tr>
<th onclick="sortTable(0)"> Team Name</th>
<th onclick="sortTable(1)">Super Bowls</th>
</tr>
<tr>
<td>Atlanta Falcons</td>
<td>0</td>
</tr>
<tr>
<td>Dallas Cowboys</td>
<td>4</td>
</tr>
<tr>
<td>Green Bay Packers</td>
<td>3</td>
</tr>
<tr>
<td>New York Giants</td>
<td>2</td>
</tr>
</table>
</div>
And this is the Javascript code:
function sortTable(n) {
var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
table = document.getElementsByClassName("supTable");
switching = true;
dir = "asc";
while (switching) {
switching = false;
rows = table.getElementsByTagName("TR");
for (i = 1; i < (rows.length - 1); i++) {
shouldSwitch = false;
x = rows[i].getElementsByTagName("TD")[n];
y = rows[i + 1].getElementsByTagName("TD")[n];
if (dir == "asc") {
if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
shouldSwitch= true;
break;
}
} else if (dir == "desc") {
if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
shouldSwitch= true;
break;
}
}
}
if (shouldSwitch) {
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
switchcount ++;
} else {
if (switchcount == 0 && dir == "asc") {
dir = "desc";
switching = true;
}
}
}
}
I think I know why the class selector doesn't work, and am happy to try a different function if it gets me the results. Can someone please help me using Javascript and ideally not jQuery, as I'm not familiar with jQuery at the moment.
I just want to be able to use the same function on several tables, so I'm not having to copy and paste the same code lots of times and changing the Id selector each time.
EDIT - A couple of people have mentioned a different question about Query selectors. How would I use query or class selectors to correctly call/solve my problem?