So, I am trying to complete one project and run into this problem: When I click any of the dropdowns, only the first one opens. I would really appreciate if someone helped me, as I am pretty new in programming.
Here is the HTML, so basically, when the user clicks on the area of "TextArrowSection" div, the dropdown shoudl open. When I coded this, I could achieve it with only one dropdown, however when I added one more, only the first one actually worked, even if I clicked on the second one.
<div class="textElement">
<div class="textArrowSection">
<p class="visibleText">How many team members can I invite?</p>
<img src="/images/icon-arrow-down.svg" class="arrowImage" alt="arrow" />
</div>
<div class="dropdown">
<p class="dropdownText closedText">
You can invite up to 2 additional users on the Free plan. There is no limit on team members for the Premium plan.
</p>
</div>
<hr class="line" />
</div>
<div class="textElement">
<div class="textArrowSection">
<p class="visibleText">How many team members can I invite?</p>
<img src="/images/icon-arrow-down.svg" class="arrowImage" alt="arrow" />
</div>
<div class="dropdown">
<p class="dropdownText closedText">
You can invite up to 2 additional users on the Free plan. There is no limit on team members for the Premium plan.
</p>
</div>
<hr class="line" />
</div>
CSS
.dropdownText {
color: #787887;
font-weight: 400;
width: 315px;
font-size: 12px;
line-height: 18px;
}
.openedText {
display: block;
}
.closedText {
display: none;
}
JavaScript
"use strict";
const clickArea = document.getElementsByClassName(`textArrowSection`);
const text = document.querySelector(`.dropdownText`);
const arrow = document.querySelector(`.arrowImage`);
const dropDown = document.querySelector(`.dropdownText`);
const closeDropDown = function () {
text.classList.remove(`openedText`);
text.classList.add(`closedText`);
arrow.classList.add(`arrowImageClosed`);
arrow.classList.remove(`arrowImageOpened`);
};
const openDropDown = function () {
text.classList.add(`openedText`);
text.classList.remove(`closedText`);
arrow.classList.add(`arrowImageOpened`);
arrow.classList.remove(`arrowImageClosed`);
};
for (var i = 0; i < clickArea.length; i++) {
clickArea[i].addEventListener(`click`, function () {
if (dropDown.classList.contains("closedText")) {
openDropDown();
} else {
closeDropDown();
}
});
}