I had create a table with two method of toggle function, first toggle is toggle table row with its specific ID and second toggle is toggle all the table row. However, if i click on first toggle at first parent row, and then when i click on second toggle, the child of first parent will hide, where it suppose follow other parent show their child and then click again to hide the child. And if all child is shown, the image also should change from details_open.png become details_close.png
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="w3.css" rel="stylesheet" />
<body>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
function toggle(thisname, image) {
var path = image.src;
var filename = path.match(/.*\/([^/]+)\.([^?]+)/i)[1];
if (filename == 'details_open') {
image.src = 'details_close.png';
}
if (filename == 'details_close') {
image.src = 'details_open.png';
}
tr = document.getElementsByTagName('tr')
for (i = 0; i < tr.length; i++) {
if (tr[i].getAttribute(thisname)) {
if (tr[i].style.display == 'none') {
tr[i].style.display = '';
} else {
tr[i].style.display = 'none';
}
}
}
}
$(function() {
$('#btn').click(function(e) {
e.preventDefault();
$('.td1').toggle();
});
});
</script>
<input type="button" id="btn" style="color:red" name=btn value="Toggle Child Accessories">
<table class="w3-table-all">
<thead>
<tr>
<td onclick="toggle(1,open_1);">
<center><img id="open_1" src="details_open.png" style="width:20px;cursor: pointer;"></center>
</td>
<td> A </td>
<td> B </td>
</tr>
</thead>
<thead>
<tr 1=fred style='display:none;' class='td1' id='td1'>
<td> </td>
<td> C </td>
<td> D </td>
</tr>
</thead>
<thead>
<tr>
<td onclick="toggle(2,open_2);">
<center><img id="open_2" src="details_open.png" style="width:20px;cursor: pointer;"></center>
</td>
<td> E </td>
<td> F</td>
</tr>
</thead>
<thead>
<tr 2=fred style='display:none;' class='td1' id='td1'>
<td> </td>
<td> G </td>
<td> H </td>
</tr>
</thead>
</table>
</body>
</html>