Design comparison
Solution retrospective
Hi there! Any feedback would be so much appreciated!
A specific javascript question I have... What is a function I can use to allow toggle to work fine but also remove all the active classes off every other question/answer section? If multiple questions are open then it breaks my layout, and initially I wrote a code that removes active classes once another question was clicked but it made the toggle function not work right.
Community feedback
- @agusc01Posted over 2 years ago
Hi Asha.
In your script.js change this
const arrows = document.querySelectorAll('.arrow'); arrows.forEach( arrow => { arrow.addEventListener('click', () =>{ let parent = arrow.parentElement; parent.parentElement.classList.toggle('active'); }) })
for this
const firstLines = document.querySelectorAll('.first-line'); firstLines.forEach((arrow) => { arrow.addEventListener('click', () => { let parent = arrow.parentElement; parent.classList.toggle('active'); }); });
Then remove function removeClasses (you didn't use it)
In your css file add this
.first-line:hover { cursor: pointer; }
For you <div class="cointainer">....</div> try to add a relative width like width:28rem for example
Try to use a border-bottom in your <div class="question-box"> instead creating a <div class="divider"></div> . Think abou this... to create a question box you need a <div> you never will forget this element because everything is inside. But you maybe create a div.question-box and forgot div.divider, then looks bad ... If you have the line divider inside the div.question-box with a border-bottom you never forget it . I know, you may think that looking at the page on a server you will notice the difference that a divider is missing, but when you spend hours and hours programming, you will not see it even if you have it in front of your eyes
Sorry for my poor english it isn't my first language.
Keep coding. ! Practices makes perfect !
Marked as helpful1@agusc01Posted over 2 years agoI just paid attention to your comment, the correct way to use the removeClasses function is
const firstLines = document.querySelectorAll('.first-line'); const questionBoxes = document.querySelectorAll('.question-box'); firstLines.forEach((firstLine) => { firstLine.addEventListener('click', () => { let questionBox = firstLine.parentElement; let wasItActive = questionBox.classList.contains('active'); removeClasses(); if (!wasItActive) { questionBox.classList.add('active'); } }); }); function removeClasses() { questionBoxes.forEach((questionBox) => { questionBox.classList.remove('active'); }); }
Marked as helpful1@livinglifemeaningPosted over 2 years ago@agusc01 Ahh! Again, thank you so much for these comments. I will go back and try to implement your suggestions. I didn't think about a bottom border instead of seperate elements, but very smart. And your English was great :)
0
Please log in to post a comment
Log in with GitHubJoin our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord