I have developed a piece of code that enables users to click on a div in order to reveal a dropdown menu containing radio buttons. My goal is to make the arrows rotate 180 degrees when the dropdown menus are opened, and then return to their original position when the dropdown menus are closed. I have inserted a few lines of code within the existing code block, and I believe I am close to solving this issue. Any advice or suggestions would be greatly appreciated. Thank you! I will provide the HTML, CSS, and JS files for review.
<main class="subscription__container">
<section
id="preferences"
class="subscription__container--preferences box"
>
<div class="question__container">
<h3 class="question__container--title">
How do you drink your coffee?
</h3>
<img
class="question__container--img"
src="../assets/plan/desktop/icon-arrow.svg"
alt="arrow"
/>
</div>
<div class="options__container">
<div class="options__container--option">
<input
id="capsule"
type="radio"
data-preference="Capsule"
value="Capsule"
name="preferences"
/>
<label class="test__trail" for="capsule">
<h4 class="options__container--title">Capsule</h4>
<p class="options__container--description">
Compatible with Nespresso systems and similar brewers.
</p>
</label>
</div>
<div class="options__container--option">
<input
id="filter"
type="radio"
data-preference="Filter"
value="Filter"
name="preferences"
/>
<label class="test__trail" for="filter">
<h4 class="options__container--title">Filter</h4>
<p class="options__container--description">
For pour over or drip methods like Aeropress, Chemex, and V60.
</p>
</label>
</div>
<div class="options__container--option">
<input
id="espresso"
type="radio"
data-preference="Espresso"
value="Espresso"
name="preferences"
/>
<label class="test__trail" for="espresso">
<h4 class="options__container--title">Espresso</h4>
<p class="options__container--description">
Dense and finely ground beans for an intense, flavorful
experience.
</p>
</label>
</div>
</div>
</section>
<section id="bean" class="subscription__container--beans box">
<div class="question__container">
<h3 class="question__container--title">What type of coffee?</h3>
<img
class="question__container--img"
src="../assets/plan/desktop/icon-arrow.svg"
alt="arrow"
/>
</div>
<div class="options__container">
<div class="options__container--option">
<input
id="single"
type="radio"
data-bean="Single"
value="Single"
name="beanType"
/>
<label class="test__trail" for="single">
<h4 class="options__container--title">Single Origin</h4>
<p class="options__container--description">
Distinct, high quality coffee from a specific family-owned
farm.
</p>
</label>
</div>
<div class="options__container--option">
<input
id="decaf"
type="radio"
data-bean="Decaf"
value="Decaf"
name="beanType"
/>
<label class="test__trail" for="decaf">
<h4 class="options__container--title">Decaf</h4>
<p class="options__container--description">
Just like regular coffee, except the caffeine has been
removed.
</p>
</label>
</div>
<div class="options__container--option">
<input
id="blended"
type="radio"
data-preference="Blended"
value="Blended"
name="beanType"
/>
<label class="test__trail" for="blended">
<h4 class="options__container--title">Blended</h4>
<p class="options__container--description">
Combination of two or three dark roasted beans of organic
coffees.
</p>
</label>
</div>
</div>
</section>
.question__container--img.rotate {
transform: rotate(180deg);
}
//
const questionBox = document.getElementsByClassName("question__container");
const arrows = document.querySelectorAll(".question__container--img");
[...questionBox].forEach((el) =>
el.addEventListener("click", (event) => {
const subMenu = event.target.parentElement.parentElement.querySelector(
".options__container"
);
subMenu.classList.toggle("open");
arrows.forEach((arrow) => arrow.classList.toggle("rotate"));
})
);
//
The current error message in the console states.. Uncaught TypeError: Cannot read property 'toggle' of undefined at HTMLDivElement. ?