Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
smriti chawla
2,566 PointsNeed someone with both HTML/CSS and Javascript expertise to help me out.
Hi guys,
I am trying to recreate the "Who is it for?" section from this webpage: http://hq.digitalmarketer.com/?_ga=1.211051989.156677951.1488792938
I am stuck with a couple of issues:
- How do I enable clicks on multiple tabs in the left column and get them to interact with their corresponding content in the right (blue) column?
- How do I ensure that only the active content of the right (blue) column is visible at once? Right now all of the content is visible, which is not how it should be.
Here's my workspace: https://w.trhou.se/jabzp8vv4o
P.S. Right now I have completed only my HTML and CSS course and a basic-level Javascript course. If there is any other course/attributes, etc. that I must complete/read about to understand how this is done, please suggest.
Appreciate all the help. Thanks in advance.
2 Answers
Vlad Legkowski
9,882 PointsHi Smriti,
I had a look at your question and I really liked this feature.
I dealt with it the following way:
I first updated the CSS and set all tab-content to not visible apart from first child
.tab-content:not(:first-child) {
display: none;
padding:15px;
}
And here is my javascript (add js folder to the root file, name the file inside app.js and add <script charset="utf-8" src="js/app.js" type="text/javascript"></script> just before the closing body tag)
const list = document.querySelectorAll('.tabs li');
const tabContent = document.querySelectorAll('.switch-area .tab-content');
list.forEach((listItem) => {
listItem.addEventListener('click', () => {
for (var i = 0; i < list.length; i++) {
list[i].classList.remove('current');
}
listItem.classList.add('current');
tabContent.forEach((tab) => {
if (listItem.dataset.tab === tab.id) {
for (var i = 0; i < tabContent.length; i++) {
tabContent[i].style.display = 'none';
}
document.getElementById(tab.id).style.display = 'inline-block';
}
})
})
});
I am sure that JS could be written in the much cleaner fashion, I am not good with it at all. But basically what I did here, is I took all of your tabs on the left and on the right and assigned them to variables, I then added event listener (click) to every item in the left tab, and on this click I remove the class "current" from every item on the left and assign same "current" class to the item I clicked on.
Then I am not sure how to say it in English, but I match "data-tab" value from the left side (written in JS as dataset.tab) with the right side tab ID's ("tab-n") and when they match, I set the matched ID with "data-tab" and set the display to inline-block to that ID. But prior to this I set display to none to all right tab elements.
Sorry, hope this makes sense.
Millan Singh
Full Stack JavaScript Techdegree Student 8,392 PointsHi Smriti.
Vlad's suggested solution looks correct though it can definitely be improved. I would recommend you complete the Javascript Basics; Javascript Loops, Arrays, and Objects; and Javascript and the DOM courses to get a well-rounded view of how you would tackle something like what you mentioned.
From a high-level perspective, you want to build a basic data model to represent the data on the page, then use event handlers to track which <li> has been clicked and set it to active (changing the data via a class or direct styling to hide or show it as needed). That should get you started.
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherHi Vlad! I've changed your comment to an answer to allow for upvoting and down voting along with possible assignment of Best Answer. Thanks for your help in the Community!