I’m currently facing an issue where I can’t get the button to toggle it’s group(which is on the same table row as is the shown button ), I want it to work on each table row’s group that exists(togging the hidden class on buttons). At this time all the buttons work, however only last group is affected which is in the last table row.
var toggleGroup = (group, group2, className = "hidden") => {
group.classList.toggle(className);
group2.classList.toggle(className);
}
var table = document.querySelector('.hour-section');
for (var i = 0, row; row = table.rows[i]; i++) {
var group = row.querySelectorAll('.temp-percent');
group.forEach(element => {
element.addEventListener("click", () => {
toggleGroup(group[0], group[1])
});
})
}
<table class="hour-section">
<tbody>
<tr>
<td>
<div class="info-group">
<div class="label">Monday</div>
<div>12 AM</div>
</div>
</td>
<td>
<img src="icons/sun.svg" class="weather-icon">
</td>
<td>
<button class="info-group temp-percent">
<div class="label">Temp</div>
<div>75°</div>
</button>
<button class="info-group temp-percent hidden">
<div class="label">Chance of Precip</div>
<div><span data-hour-precip>75</span> %</div>
</button>
</td>
<td>
<div class="info-group">
<div class="label">FL Temp</div>
<div>75°</div>
</div>
</td>
<td>
<div class="info-group">
<div class="label">Wind</div>
<div>7 <span class="value-sub-info">mph</span></div>
</div>
</td>
<td>
<div class="info-group">
<div class="label">Precip</div>
<div>7 <span class="value-sub-info">in</span></div>
</div>
</td>
<td>
<div class="info-group">
<div class="label">Dew Point</div>
<div>50°</div>
</div>
</td>
<td>
<div class="info-group">
<div class="label">Humidity</div>
<div>
<span data-hour-humidity>75</span>
<span class="value-sub-info">%</span>
</div>
</div>
</td>
</tr>
<tr>
<td>
<div class="info-group">
<div class="label">Monday</div>
<div>12 AM</div>
</div>
</td>
<td>
<img src="icons/sun.svg" class="weather-icon">
</td>
<td>
<button class="info-group temp-percent">
<div class="label">Temp</div>
<div>75°</div>
</button>
<button class="info-group temp-percent hidden">
<div class="label">Chance of Precip</div>
<div><span data-hour-precip>75</span> %</div>
</button>
</td>
<td>
<div class="info-group">
<div class="label">FL Temp</div>
<div>75°</div>
</div>
</td>
<td>
<div class="info-group">
<div class="label">Wind</div>
<div>7 <span class="value-sub-info">mph</span></div>
</div>
</td>
<td>
<div class="info-group">
<div class="label">Precip</div>
<div>7 <span class="value-sub-info">in</span></div>
</div>
</td>
<td>
<div class="info-group">
<div class="label">Dew Point</div>
<div>50°</div>
</div>
</td>
<td>
<div class="info-group">
<div class="label">Humidity</div>
<div>
<span data-hour-humidity>75</span>
<span class="value-sub-info">%</span>
</div>
</div>
</td>
</tr>
<tr>
<td>
<div class="info-group">
<div class="label">Monday</div>
<div>12 AM</div>
</div>
</td>
<td>
<img src="icons/sun.svg" class="weather-icon">
</td>
<td>
<button class="info-group temp-percent">
<div class="label">Temp</div>
<div>75°</div>
</button>
<button class="info-group temp-percent hidden">
<div class="label">Chance of Precip</div>
<div><span data-hour-precip>75</span> %</div>
</button>
</td>
<td>
<div class="info-group">
<div class="label">FL Temp</div>
<div>75°</div>
</div>
</td>
<td>
<div class="info-group">
<div class="label">Wind</div>
<div>7 <span class="value-sub-info">mph</span></div>
</div>
</td>
<td>
<div class="info-group">
<div class="label">Precip</div>
<div>7 <span class="value-sub-info">in</span></div>
</div>
</td>
<td>
<div class="info-group">
<div class="label">Dew Point</div>
<div>50°</div>
</div>
</td>
<td>
<div class="info-group">
<div class="label">Humidity</div>
<div>
<span data-hour-humidity>75</span>
<span class="value-sub-info">%</span>
</div>
</div>
</td>
</tr>
<tr>
<td>
<div class="info-group">
<div class="label">Monday</div>
<div>12 AM</div>
</div>
</td>
<td>
<img src="icons/sun.svg" class="weather-icon">
</td>
<td>
<button class="info-group temp-percent">
<div class="label">Temp</div>
<div>75°</div>
</button>
<button class="info-group temp-percent hidden">
<div class="label">Chance of Precip</div>
<div><span data-hour-precip>75</span> %</div>
</button>
</td>
<td>
<div class="info-group">
<div class="label">FL Temp</div>
<div>75°</div>
</div>
</td>
<td>
<div class="info-group">
<div class="label">Wind</div>
<div>7 <span class="value-sub-info">mph</span></div>
</div>
</td>
<td>
<div class="info-group">
<div class="label">Precip</div>
<div>7 <span class="value-sub-info">in</span></div>
</div>
</td>
<td>
<div class="info-group">
<div class="label">Dew Point</div>
<div>50°</div>
</div>
</td>
<td>
<div class="info-group">
<div class="label">Humidity</div>
<div>
<span data-hour-humidity>75</span>
<span class="value-sub-info">%</span>
</div>
</div>
</td>
</tr>
</tbody>
</table>
2
Answers
It seems that the issue is related to the usage of arrow functions inside the event listener, which is causing the last group to be affected when any of the buttons are clicked. To fix this issue, you can change the arrow function to a regular function and use the this keyword. Also, since you are adding an event listener to both buttons in the group, you can use only one forEach loop for all rows. Here’s the updated JavaScript code:
There is a very simple solution to this which is to change some
var
here toconst
orlet
AND put alet
in the loop iterator. Note I also put(event)
in there which I used to illustrate the scope ofthis
and thus the use the theevent.target
;Note I used the border only to illustrate what has that class currently.
Second example with different use which is probably how I would have done this: note the simpler loop and use of the target to get the container parent and the other button in that context